xaml DataGrid绑定到字典中的字典中的字典

时间:2013-04-12 13:04:38

标签: c# xaml dictionary datagrid

我想将xaml DataGrid绑定到包含类似这样的东西的模型:

TheModel{
  instanceOfClassA
}

ClassA{
  someProps
  Dictionary<string, classB>
}

ClassB{
  someOtherProps
  Dictionary<string, classC>
}

ClassC{
  someMoreProps
}

这意味着在DataGrid中,在ClassA的ClassB中将有一行foreach ClassC,其中列包含所有三个dictionarys的数据。

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

我可以使用xaml元素吗?

由于 菲利普。

1 个答案:

答案 0 :(得分:1)

我认为您需要将数据表示为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

如果您想要展开/折叠功能,请查看此链接。

Displaying hierarchal parent child data in WPF DataGrid

如果您可以访问infragistics控件,我强烈建议您使用XamDataGrid而不是.NET DataGrid。你几乎可以用这种控制做任何你想做的事。

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

更新

对于平面数据,您可以像这样为模型创建包装器。

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