Dictionaryを含むモデルのデータコレクションをバインド
そもそもDictionaryがいけるのかな?と調査。IEnumerable
Window1.xml.cs
public Window1() { InitializeComponent(); this.DataContext = new List<Model>() { new Model() { Title = "Header1", Attributes = new Dictionary<string, IEnumerable<string>>() { {"Key1", new List<string>{ "Value1", "Value2", "Value3", "Value4"}}, {"Key2", new List<string>{ "Value5", "Value6"}}, {"Key3", new List<string>{ "Value7", "Value8", "Value9"}}, } }, new Model() { Title = "Header2", Attributes = new Dictionary<string, IEnumerable<string>>() { {"Key4", new List<string>{ "Value1"}}, {"Key5", new List<string>{ "Value2", "Value3", "Value4", "Value5", "Value6", "Value7"}}, {"Key6", new List<string>{ "Value8", "Value9"}}, } }, }; } class Model { public string Title { get; set; } public Dictionary<string, IEnumerable<string>> Attributes { get; set; } }
Window1.xml
<ItemsControl ItemsSource="{Binding}"> <ItemsControl.ItemTemplate> <DataTemplate> <Expander Header="{Binding Title}"> <ItemsControl ItemsSource="{Binding Attributes}"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Border BorderThickness="2" BorderBrush="Green"> <TextBlock Text="{Binding Key}" Foreground="Green"/> </Border> <Border BorderThickness="2" BorderBrush="Blue"> <ItemsControl ItemsSource="{Binding Value}"> <ItemsControl.ItemTemplate> <DataTemplate> <Border BorderThickness="2" BorderBrush="Red"> <TextBlock Text="{Binding}" Foreground="Green"/> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Border> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Expander> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>