質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

1回答

816閲覧

Xamarin.FormsでListViewを動的に作成したい。

terahotty

総合スコア1

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2023/04/23 05:02

実現したいこと

Xamarin.FormsでListViewを動的に作成したい。

前提

ListViewの列を可変にする必要があり、その前段としてListViewをコードで作成しようとしています。

発生している問題・エラーメッセージ

実行すると、ItemsSourceにセットしたデータが正しく表示されない。
10行のデータをセットすると、1~9行まで同じ値が表示される。

該当のソースコード

Xaml

1 <ContentPage.Content> 2 <ListView x:Name="listData" ItemsSource="{Binding listviewItems}"> 3 <ListView.ItemTemplate> 4 <DataTemplate> 5 <ViewCell> 6 </ViewCell> 7 </DataTemplate> 8 </ListView.ItemTemplate> 9 </ListView> 10 </ContentPage.Content>

C#

1 public class gridData 2 { 3 public int rownum { get; set; } 4 public string item1 { get; set; } 5 public string item2 { get; set; } 6 } 7 8 public List<gridData> GetData() 9 { 10 List<gridData> lst = new List<gridData>(); 11 12 for (int i = 0; i < 10; i++) 13 { 14 lst.Add(new gridData() 15 { 16 rownum = i + 1, 17 item1 = "item1-" + i.ToString(), 18 item2 = "item2-" + i.ToString(), 19 }); 20 } 21 return lst; 22 } 23 24 public Page1() 25 { 26 InitializeComponent(); 27 28 // テンプレートを初期化 29 this.BasicGridPageCS(); 30 } 31 32 protected async override Task<bool> OnAppearingFirstCallOnly() 33 { 34 List<gridData> lst = GetData(); 35 this.listData.ItemsSource = lst; 36 return true; 37 } 38 39 public void BasicGridPageCS() 40 { 41 Grid grid = new Grid 42 { 43 Padding = new Thickness(0, 0, 0, 0), 44 RowSpacing = 1, 45 ColumnSpacing = 1, 46 RowDefinitions = 47 { 48 new RowDefinition { Height = new GridLength(0.1, GridUnitType.Star) }, 49 }, 50 ColumnDefinitions = 51 { 52 new ColumnDefinition { Width = new GridLength(0.050, GridUnitType.Star) }, 53 new ColumnDefinition { Width = new GridLength(0.050, GridUnitType.Star) }, 54 new ColumnDefinition { Width = new GridLength(0.130, GridUnitType.Star) }, 55 }, 56 }; 57 58 grid.Children.Add(getLabelDetail("rownum", LayoutOptions.EndAndExpand), 0, 0); 59 grid.Children.Add(getLabelDetail("item1", LayoutOptions.StartAndExpand), 1, 0); 60 grid.Children.Add(getLabelDetail("item2", LayoutOptions.StartAndExpand), 2, 0); 61 62 DataTemplate dataTemplate = new DataTemplate(() => { 63 return new ViewCell() { View = grid }; 64 }); 65 this.listData.ItemTemplate = dataTemplate; 66 } 67 private Label getLabelDetail(string bind, LayoutOptions layoutH) 68 { 69 Label lbl = new Label(); 70 lbl.BackgroundColor = Color.Transparent; 71 lbl.FontSize = MbStdFontSize.Small; 72 lbl.TextColor = Color.Black; 73 lbl.HorizontalOptions = layoutH; 74 lbl.VerticalOptions = LayoutOptions.Center; 75 lbl.Margin = new Thickness(1, 0, 1, 0); 76 lbl.SetBinding( 77 Label.TextProperty, 78 bind, 79 BindingMode.OneWay, 80 null, 81 null); 82 return lbl; 83 }

試したこと

https://learn.microsoft.com/ja-jp/xamarin/xamarin-forms/user-interface/layouts/grid
このページを見てGRIDを作りました。
バインドがうまくできていないと思うのですが。。。
lbl.SetBinding()では、各行の値にバインドできないのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。
バインディング コンテキストの変更処理が必要でした。
https://learn.microsoft.com/ja-jp/xamarin/xamarin-forms/user-interface/listview/customizing-cell-appearance

ViewCellクラスを作成し、BindablePropertyを定義して、必要な列をグリッドに追加するようにしたら、ItemsSourceにセットしたデータが一覧表示されました。
表示したくない列幅を0にしても、同じ見た目になります。

一応コードを貼っておきます。

c#

1 public static List<bool> gColVisible = new List<bool>() { true, false, true }; 2 3 public void BasicGridPageCS() 4 { 5 var customCell = new DataTemplate(typeof(CustomCell)); 6 customCell.SetBinding(CustomCell.RownumProperty, "rownum"); 7 customCell.SetBinding(CustomCell.Item1Property, "item1"); 8 customCell.SetBinding(CustomCell.item2Property, "item2"); 9 listData.ItemTemplate = customCell; 10 } 11 12 public class CustomCell : ViewCell 13 { 14 Label rownumLabel, item1Label, item2Label; 15 16 public static readonly BindableProperty RownumProperty = 17 BindableProperty.Create("rownum", typeof(int), typeof(ViewCell), 0); 18 public static readonly BindableProperty Item1Property = 19 BindableProperty.Create("item1", typeof(string), typeof(ViewCell), "item1"); 20 public static readonly BindableProperty item2Property = 21 BindableProperty.Create("item2", typeof(string), typeof(ViewCell), "item2"); 22 23 public int Rownum 24 { 25 get { return (int)GetValue(RownumProperty); } 26 set { SetValue(RownumProperty, value); } 27 } 28 29 public string Item1 30 { 31 get { return (string)GetValue(Item1Property); } 32 set { SetValue(Item1Property, value); } 33 } 34 35 public string Item2 36 { 37 get { return (string)GetValue(item2Property); } 38 set { SetValue(item2Property, value); } 39 } 40 41 protected override void OnBindingContextChanged() 42 { 43 base.OnBindingContextChanged(); 44 45 if (BindingContext != null) 46 { 47 if (gColVisible[0]) 48 { 49 rownumLabel.Text = Rownum.ToString(); 50 } 51 if (gColVisible[1]) 52 { 53 item1Label.Text = Item1; 54 } 55 if (gColVisible[2]) 56 { 57 item2Label.Text = Item2; 58 } 59 } 60 } 61 62 public CustomCell() 63 { 64 Grid grid = new Grid 65 { 66 Padding = new Thickness(0, 0, 0, 0), 67 RowSpacing = 1, 68 ColumnSpacing = 1, 69 RowDefinitions = 70 { 71 new RowDefinition { Height = new GridLength(0.1, GridUnitType.Star) }, 72 }, 73 ColumnDefinitions = columnDefinitionsList(), 74 }; 75 76 int col = 0; 77 78 if (gColVisible[0]) 79 { 80 rownumLabel = getLabelDetail("rownum", LayoutOptions.EndAndExpand); 81 grid.Children.Add(rownumLabel, col, 0); 82 col += 1; 83 } 84 if (gColVisible[1]) 85 { 86 item1Label = getLabelDetail("item1", LayoutOptions.StartAndExpand); 87 grid.Children.Add(item1Label, col, 0); 88 col += 1; 89 } 90 if (gColVisible[2]) 91 { 92 item2Label = getLabelDetail("item2", LayoutOptions.StartAndExpand); 93 grid.Children.Add(item2Label, col, 0); 94 col += 1; 95 } 96 97 View = grid; 98 } 99 100 private ColumnDefinitionCollection columnDefinitionsList() 101 { 102 ColumnDefinitionCollection lst = new ColumnDefinitionCollection(); 103 104 if (gColVisible[0]) 105 { 106 lst.Add(new ColumnDefinition { Width = new GridLength(0.050, GridUnitType.Star) }); 107 } 108 if (gColVisible[1]) 109 { 110 lst.Add(new ColumnDefinition { Width = new GridLength(0.050, GridUnitType.Star) }); 111 //lst.Add(new ColumnDefinition { Width = new GridLength(0, GridUnitType.Absolute) }); 112 } 113 if (gColVisible[2]) 114 { 115 lst.Add(new ColumnDefinition { Width = new GridLength(0.130, GridUnitType.Star) }); 116 } 117 118 return lst; 119 } 120 121 public Label getLabelDetail(string bind, LayoutOptions layoutH) 122 { 123 Label lbl = new Label(); 124 lbl.BackgroundColor = Color.Transparent; 125 lbl.FontSize = MbStdFontSize.Small; 126 lbl.TextColor = Color.Black; 127 lbl.HorizontalOptions = layoutH; 128 lbl.VerticalOptions = LayoutOptions.Center; 129 lbl.Margin = new Thickness(1, 0, 1, 0); 130 lbl.SetBinding( 131 Label.TextProperty, 132 bind, 133 BindingMode.OneWay, 134 null, 135 null); 136 return lbl; 137 } 138 } 139

投稿2023/04/23 12:19

terahotty

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問