I开发者_运维问答 have a Datagrid, on start up I set its set of columns then I bind it to a List> in order to fill it with data. Each nested list has the same number of items as the number of columns I got, but... I don't get nothing shown... What's the correct practice to bind a 2D collection to a DataGrid ?
Code Sample
List<List<String>> rows = SomeFunctionThatReturnsTheRows();
this.grid.ItemsSource = rows;
Thank you, Miloud B.
then how about this
var rows = new List<List<string>>()
{
new List<string>() {"List1-1", "List1-2", "List1-3"},
new List<string>() {"List2-1", "List2-2", "List2-3"}
};
GridView gv = new GridView();
this.grid.View = gv;
gv.Columns.Add(new GridViewColumn(){DisplayMemberBinding = new Binding(".[0]")});
gv.Columns.Add(new GridViewColumn(){DisplayMemberBinding = new Binding(".[1]")});
gv.Columns.Add(new GridViewColumn(){DisplayMemberBinding = new Binding(".[2]")});
this.grid.ItemsSource = rows;
try this
DataContext = new List<List<string>>()
{
new List<string>() {"List1-1", "List1-2", "List1-3"},
new List<string>() {"List2-1", "List2-2", "List2-3"}
};
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding .[0]}" />
<GridViewColumn DisplayMemberBinding="{Binding .[1]}" />
<GridViewColumn DisplayMemberBinding="{Binding .[2]}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
produces this
What you need to google for is binding to a nested collection, rather than 2D collection - you'll get more results that way :)
Does WPF DataGrid: DataGridComboxBox ItemsSource Binding to a Collection of Collections answer your question?
精彩评论