我有一个这样的类:
public class UIThing {
public string Name{get;set}
public IEnumerable<Thing> LotsOfThings;
}我在List (List<UIThings>)中有其中的一些,我想将它们绑定到一个ListBox中,这样LotsOfThings成员就可以作为ListBox中的项展开。我猜就像是列表的列表。但我无法理解所需的DataTemplate。
有什么想法吗?
发布于 2013-04-03 01:35:49
这可能会让你有这样做的想法:
我建议您使用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;
}这是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>结果:

编辑:这里的是ListBox版本
<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>https://stackoverflow.com/questions/15769491
复制相似问题