WPF:绑定集合与集合与组织一个ListBox [英] WPF: Bind Collection with Collection to a ListBox with groups
问题描述
有时WPF对我来说太复杂了。我有我的窗口1持有集团S集合。 本集团与人的个集合类。到底这应该是一个联系人列表。我只是想要做的是显示其人组在ListBox,其中列表组的组名等于我的班组的名称属性。
sometimes WPF is too complex for me. I've got my "Window1" holding a collection of "Group"s. "Group" is a class with a collection of "Person"s. In the end this should be a contact list. What I simply want to do is to show the groups with its person in a ListBox, where the group name of the list groups equals the Name Property of my class "Groups".
我试着绑定到收集一CollectionViewSource。组示出正确的,但是该列表中的项目都等于组名称。因此,每个组只有一个项目:它的组名
I've tried with a CollectionViewSource bound to the "Collection". The groups are shown correct, but the items of the list are equal to the group names. So each group has only one item: its group name.
这里的许多例子显示的项目只有一个集合分组。我所能做的就是设置组名为人的属性。但我不能指望(这是真的neccessary):
- 有多少人在每个组
- 如何的许多人有状态,在线
Many examples here show the grouping of items with only one collection. What I can do is to set the group name as Property of "Person". But then I can't count (and that is really neccessary): - how many persons are in each group - how many of that persons have the "Status" "Online".
我使用LINQ在组级来算的。
感谢您的任何意见帮助我上手。
I use linq in the "Group" class to count that. Thanks for any advice helping me to get started.
推荐答案
好吧,我不知道如果这是你想要实现但这里是一种方式,你可以试试:
Well, I am not sure if this is what you want achieve but here is a way that you can try:
假设你的类是这样的:
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
作为另一个列表框绑定到联系人属性,如:
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.
修改:使用另一种解决方案 TreeView控件
Edit: Another solution by using 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>
这篇关于WPF:绑定集合与集合与组织一个ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!