我想将xaml DataGrid绑定到包含如下内容的模型:
TheModel{
instanceOfClassA
}
ClassA{
someProps
Dictionary<string, classB>
}
ClassB{
someOtherProps
Dictionary<string, classC>
}
ClassC{
someMoreProps
}
这意味着在DataGrid中,ClassA中的ClassB中的ClassC的每一行都将有一行,其中包含所有三个字典的数据的列。
我不想在Model内部实现TOList方法,因为那样会破坏Model与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/