我有一个Datagrid,在启动时我设置了它的列集然后我将它绑定到List>为了填充数据。每个嵌套列表与我获得的列数具有相同数量的项目,但是......我没有得到任何结果......将2D集合绑定到DataGrid的正确做法是什么?
代码示例
List<List<String>> rows = SomeFunctionThatReturnsTheRows();
this.grid.ItemsSource = rows;
谢谢你, 米卢德B.
答案 0 :(得分:2)
然后呢
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;
答案 1 :(得分:1)
你需要google for绑定到嵌套集合,而不是2D集合 - 你会得到更多结果:)
WPF DataGrid: DataGridComboxBox ItemsSource Binding to a Collection of Collections会回答您的问题吗?
答案 2 :(得分:1)
试试这个
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>
产生这个