c# - xaml DataGrid 绑定(bind)到字典中的字典中的字典

标签 c# xaml dictionary datagrid

我想将 xaml DataGrid 绑定(bind)到包含类似内容的模型:

TheModel{
  instanceOfClassA
}

ClassA{
  someProps
  Dictionary<string, classB>
}

ClassB{
  someOtherProps
  Dictionary<string, classC>
}

ClassC{
  someMoreProps
}

这意味着在 DataGrid 中,对于 ClassA 中的 ClassB 中的每个 ClassC 都有一行,其中的列包含所有三个字典的数据。

我不想在模型中实现 TOList 方法,因为那样会破坏模型和 View 之间的分离。

有我可以使用的 xaml 元素吗?

谢谢 菲利普。

最佳答案

我认为您需要将数据表示为 DataGrid 中的分层行。你可以这样做。

<DataGrid AutoGenerateColumns="False"
          ItemsSource="{Binding Data}"
          RowDetailsVisibilityMode="Visible">

  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding P1}" />
    <DataGridTextColumn Binding="{Binding P2}" />
  </DataGrid.Columns>

  <DataGrid.RowDetailsTemplate>

    <DataTemplate>

      <DataGrid AutoGenerateColumns="False"
                ItemsSource="{Binding DictionaryInA.Values}"
                RowDetailsVisibilityMode="Visible">

        <DataGrid.RowDetailsTemplate>

          <DataTemplate>

            <DataGrid AutoGenerateColumns="False"
                      ItemsSource="{Binding DictionaryInB.Values}">

              <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding P1}" />
                <DataGridTextColumn Binding="{Binding P2}" />
              </DataGrid.Columns>

            </DataGrid>

          </DataTemplate>

        </DataGrid.RowDetailsTemplate>

        <DataGrid.Columns>
          <DataGridTextColumn Binding="{Binding P1}" />
          <DataGridTextColumn Binding="{Binding P2}" />
        </DataGrid.Columns>

      </DataGrid>
    </DataTemplate>
  </DataGrid.RowDetailsTemplate>
</DataGrid>

您的数据将如下所示。

- Class A Row 1
  - Class B Row 1
      - Class C Row 1
      - Class C Row 2
      - Class C Row N
  - Class B Row 2
      - Class C Row 1
      - Class C Row 2
      - Class C Row N
  - Class B Row N
      Class C Row 1
      Class C Row 2
      Class C Row N
- Class A Row 2
  - Class B Row 1
      - Class C Row 1
      - Class C Row 2
      - Class C Row N
  - Class B Row 2
      - Class C Row 1
      - Class C Row 2
      - Class C Row N
  - Class B Row N
      Class C Row 1
      Class C Row 2
      Class C Row N
- Class A Row N
  - Class B Row 1
      - Class C Row 1
      - Class C Row 2
      - Class C Row N
  - Class B Row 2
      - Class C Row 1
      - Class C Row 2
      - Class C Row N
  - Class B Row N
      Class C Row 1
      Class C Row 2
      Class C Row N

If you want expand/collapse functionality, take a look at this link.

Displaying hierarchal parent child data in WPF DataGrid

If you have access to infragistics controls, I would highly recommend using the XamDataGrid instead of the .NET DataGrid. You can pretty much do anything you want with that control.

http://www.infragistics.com/products/wpf/data-grid/

Update

For flat data, you can can create a wrapper for your model like this.

public IEnumerable FlattenedModel
{
    get
    {
        return (from b in TheModel.InstanceOfClassA.DictionaryInA.Values
                from c in b.DictionaryInB.Values
                select new
                {
                    PropertyA1 = TheModel.PropertyA1,
                    PropertyA2 = TheModel.PropertyA2,
                    PropertyB1 = b.PropertyB1,
                    PropertyB2 = b.PropertyB2,
                    PropertyC1 = c.PropertyC1,
                    PropertyC2 = c.PropertyC2
                }).ToList();
    }
}

如果您不能做包装器,那么转换器也可以。

public class FlattenTheModelConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var InstanceOfTheModel = value as TheModel;

        return (from b in InstanceOfTheModel.InstanceOfClassA.DictionaryInA.Values
                from c in b.DictionaryInB.Values
                select new
                {
                    PropertyA1 = InstanceOfTheModel .PropertyA1,
                    PropertyA2 = InstanceOfTheModel .PropertyA2,
                    PropertyB1 = b.PropertyB1,
                    PropertyB2 = b.PropertyB2,
                    PropertyC1 = c.PropertyC1,
                    PropertyC2 = c.PropertyC2
                }).ToList();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如果您决定使用转换器,则需要按如下方式修改 XAML。

<DataGrid ItemsSource="{Binding TheModel, Converter={StaticResource FlattenTheModelConverter}, Mode=OneWay}">

关于c# - xaml DataGrid 绑定(bind)到字典中的字典中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15972167/

相关文章:

c# - 程序化 RDP 登录和 SendMessage

WPF:列表框单击并拖动选择其他项目

wpf - 如何使用 Trigger 检查 DataGridColumnHeader 的 ColumnIndex 是否是最后一个?

java - 不能对 HashMap 中的每个

C# 替换为混合值

c# - 如何获取绑定(bind)对象的属性?

wpf - 表达混合书籍?

c# - 如何在 C# 中比较两个字典

java - 如何以MapReduce格式在一行中打印一些 token ?

c# - 仅返回最后一次迭代的值