[英]xaml DataGrid bind to dictionary in dictionary in dictionary
我想將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
如果要擴展/折疊功能,請查看此鏈接。
如果可以訪問基礎設施控件,則強烈建議使用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}">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.