更新时间:2023-10-06 13:43:10
好吧,我不确定这是否是您想要实现的目标,但您可以尝试以下方法:
Well, I am not sure if this is what you want achieve but here is a way that you can try:
假设你的课程是这样的:
Assuming your classes are like these:
public class Group
{
public string Name { get; set; }
public List<Contact> Contacts { get; set; }
}
public class Contact
{
public string Name { get; set; }
public bool IsOnline { get; set; }
}
您可以将 ListBox.ItemTemplate
设置为另一个 ListBox 绑定到 Contacts 属性,例如:
You can set ListBox.ItemTemplate
as another ListBox bind to Contacts property, like:
<CollectionViewSource x:Key="groups" Source="{Binding}" >
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Name" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="groupTemplate" DataType="Group">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<ListBox ItemsSource="{Binding Source={StaticResource groups}}">
<ListBox.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource groupTemplate}" />
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="Contact">
<ListBox ItemsSource="{Binding Contacts}">
<ListBox.ItemTemplate>
<DataTemplate DataType="Contact">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您必须稍微设置内部列表框的样式.
You have to style the inner listbox a little bit.
Edit:使用TreeView
<DataTemplate DataType="Contact">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
<TreeView ItemsSource="{Binding Source={StaticResource groups}}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding Contacts}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>