我想将 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/