绑定Datagrid中的多个列表

时间:2017-09-22 18:02:43

标签: wpf xaml

我需要达到这样的结果:

enter image description here

xaml不允许通过代码创建行,因此我将行逻辑实现为列表。每个清单都是一行。

我基本上做的是:

LastFiveHomeAwayMatches2 = new List<List<string>>()
                {
                  new List<string> {"Accrington", "D", "D", "W", "W", "W", "11" },
                  new List<string> { string.Empty, "1-1", "0-0", "0-1", "4-2", "0-3", string.Empty },
                  new List<string> {"Fos", "D", "D", "W", "W", "W", "11" },
                  new List<string> { string.Empty, "1-1", "0-0", "0-1", "4-2", "0-3", string.Empty },
                };

但如何在Datagrid中绑定此多个列表以实现显示的结果?谢谢。 的更新

<DataGrid AutoGenerateColumns="False" 
         ItemsSource="{Binding MatchController.LatestFiveMatches}">
            <DataGrid.Columns>
                   <DataGridTextColumn Header="Teams" />
                                    <DataGridTextColumn Header="5" />
                                    <DataGridTextColumn Header="4" />
                                    <DataGridTextColumn Header="3" />
                                    <DataGridTextColumn Header="2" />
                                    <DataGridTextColumn Header="1" />
                                    <DataGridTextColumn Header="Pt."/>
                                </DataGrid.Columns>

                            </DataGrid>

这不会显示任何内容。如果datagrid为空,我还需要显示为标题列Teams, 5, 4, 3, 2, 1

1 个答案:

答案 0 :(得分:1)

List<string>替换为您要显示的每列具有公共属性的类型,例如:

LastFiveHomeAwayMatches2 = new List<RowType>()
            {
              new RowType { Team = "Accrington", A = "D", B = "D", C = "W", D = "W", E = "W", F = "11" },
              ...
            };
dataGrid.ItemsSource = LastFiveHomeAwayMatches2;
public class RowType
{
    public string Team { get; set; }
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D { get; set; }
    public string E { get; set; }
    public string F { get; set; }
}

编辑:

另请注意,您必须将每列绑定到相应的属性:

<DataGridTextColumn Header="Teams" Binding="{Binding Team}" />