C# WPF:将带有集合的集合绑定到带有组的列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/896587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: Bind Collection with Collection to a ListBox with groups
提问by
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".
有时 WPF 对我来说太复杂了。我有我的“Window1”持有“Group”的集合。“Group”是一个包含“Person”集合的类。最后,这应该是一个联系人列表。我只想做的是在 ListBox 中显示组及其人员,其中列表组的组名等于我的类“组”的名称属性。
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.
我试过将 CollectionViewSource 绑定到“集合”。组显示正确,但列表项与组名相同。所以每个组只有一个项目:它的组名。
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".
这里的许多示例显示了只有一个集合的项目分组。我能做的是将组名设置为“人”的属性。但是我无法计算(这确实是必要的): - 每个组中有多少人 - 其中有多少人具有“状态”“在线”。
I use linq in the "Group" class to count that. Thanks for any advice helping me to get started.
我在“Group”类中使用 linq 来计算它。感谢您提供帮助我入门的任何建议。
采纳答案by idursun
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; }
}
You can set ListBox.ItemTemplate
as another ListBox bind to Contacts property, like:
您可以设置ListBox.ItemTemplate
为另一个 ListBox 绑定到 Contacts 属性,例如:
<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: Another solution by using TreeView
编辑:使用的另一种解决方案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>
回答by Gishu
If you have a copy of Programming WPF, jump to Page 221. If not I'll summarize (in my inept sort of way)
如果您有Programming WPF的副本,请跳到第 221 页。如果没有,我将总结(以我无能的方式)
First you don't need to manually group the Person objects.
首先,您不需要手动对 Person 对象进行分组。
If each Person object has a Group property,
如果每个 Person 对象都有一个 Group 属性,
People people = // retrieve the List<Person> somehow
ICollectionView view = CollectionViewSource.GetDefaultView(people);
view.GroupDescriptions.Add( new PropertyGroupDescription("Group") );
All controls that derive from ItemsControl can display grouped items, so
从 ItemsControl 派生的所有控件都可以显示分组的项目,因此
// the XAML
<ListBox ... ItemsSource={Binding}>
<ListBox.GroupStyle>
<x:Static Member="GroupStyle.Default" />
</ListBox.GroupStyle>
</ListBox>
You can also define a custom GroupStyle and specify the GroupStyle.HeaderTemplate to define a new DataTemplate that shows custom attributes like 'some PropertyValue (Count)' - Bind the one TextBlock to {Binding SomeProperty}
and the other to {Binding ItemCount}
您也可以自定义一个GroupStyle并指定GroupStyle.HeaderTemplate定义一个新的DataTemplate该节目自定义属性,如“一些的PropertyValue(计数)” -绑定一个的TextBlock到{Binding SomeProperty}
与另{Binding ItemCount}
HTH
HTH