C# xaml DataGrid绑定到dictionary中dictionary中的dictionary
我想将xaml DataGrid绑定到包含以下内容的模型:C# xaml DataGrid绑定到dictionary中dictionary中的dictionary,c#,xaml,dictionary,datagrid,C#,Xaml,Dictionary,Datagrid,我想将xaml DataGrid绑定到包含以下内容的模型: TheModel{ instanceOfClassA } ClassA{ someProps Dictionary<string, classB> } ClassB{ someOtherProps Dictionary<string, classC> } ClassC{ someMoreProps } TheModel{ 实例FClassa } 甲级{ 一些道具 字典 } B类{
TheModel{
instanceOfClassA
}
ClassA{
someProps
Dictionary<string, classB>
}
ClassB{
someOtherProps
Dictionary<string, classC>
}
ClassC{
someMoreProps
}
TheModel{
实例FClassa
}
甲级{
一些道具
字典
}
B类{
其他道具
字典
}
C类{
更多道具
}
这意味着在DataGrid中,ClassA中ClassB中的每个ClassC都有一行,列包含所有三个字典的数据
我不想在模型内部实现TOList方法,因为这会打破模型和视图之间的分离
我可以使用xaml元素吗
谢谢
Philipp.我认为您需要在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
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}">
Hmm我不想添加或显示行详细信息。。我只想将我的数据层次结构扁平化为一个数据网格。更新了答案,包括如何转换和显示扁平数据的信息。