I have a class like this:
public class UIThing {
public string Name{get;set}
public IEnumerable<Thing> LotsOfThings;
}
I have some of these in a List
(List<UIThings>
) and I would like to bind them into a ListBox
so that the LotsOfThings member is expanded as items in the ListBox
. Like a list of lists I guess. But I can't get my head around the DataTemplate
needed.
Any Ideas?
This may give you the idea to do it:
I recommend you to use ItemsControl
public class UIThing
{
public string Name { get; set; }
public List<string> LotsOfThings { get; set; }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
UIThing item1 = new UIThing() { Name = "A", LotsOfThings = new List<string>() { "1A", "2A", "3A", "4A" } };
UIThing item2 = new UIThing() { Name = "B", LotsOfThings = new List<string>() { "1B", "2B", "3B", "4B" } };
UIThing item3 = new UIThing() { Name = "C", LotsOfThings = new List<string>() { "1C", "2C", "3C", "4C" } };
UIThing item4 = new UIThing() { Name = "D", LotsOfThings = new List<string>() { "1D", "2D", "3D", "4D" } };
var list = new List<UIThing>() { item1, item2, item3, item4 };
itemsControl.ItemsSource = list;
}
And this is the XAML:
<ItemsControl Name="itemsControl" HorizontalAlignment="Left" Width="100">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Name}" Margin="0,5" Width="auto">
<ListBox Width="auto" ItemsSource="{Binding LotsOfThings}" Margin="20,0,0,0" Background="AliceBlue"></ListBox>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Result:
EDIT: here is the ListBox version
<ListBox Name="itemsControl" Margin="0,0,197,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Name}" Margin="0,5"></Label>
<Border Margin="20,0,0,0" Background="AliceBlue" CornerRadius="10">
<ListBox Width="auto" ItemsSource="{Binding LotsOfThings}" ></ListBox>
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>