【问题标题】:Binding multiple list in Datagrid在Datagrid中绑定多个列表
【发布时间】:2017-09-22 18:02:42
【问题描述】:

我需要达到这样的结果:

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>

这不显示任何内容。如果数据网格为空白,我也需要显示为标题列 Teams, 5, 4, 3, 2, 1

【问题讨论】:

  • 这是我第四次看到这个问题的措辞略有不同。叹。考虑使用用您的值填充行的 DataTable(这里有一个简单的示例:stackoverflow.com/a/44206066/1506454)。禁用 DataGrid 排序,否则行可以重新排序
  • 似乎有些 stackoverflow 用户讨厌我,idk,我因为一个很好解释的问题而被禁止,所以不知道。

标签: wpf xaml


【解决方案1】:

List&lt;string&gt; 替换为对您要显示的每一列都有公共属性的类型,例如:

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}" />

【讨论】:

  • 这是一个优雅而简单的解决方案,谢谢。只需一个问题,我需要向用户显示空白列Teams, 5, 4, 3, 2, 1, Pt. 我在问题中添加了一个更新,以向您展示我的数据网格,无论如何,似乎没有显示任何内容。
  • 查看我的编辑。您需要将每一列绑定到 RowType 类的相应属性。
猜你喜欢
  • 2016-02-18
  • 2015-01-02
  • 1970-01-01
  • 2014-08-13
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多