WPF:将集合与集合绑定到具有组的列表框 [英] WPF: Bind Collection with Collection to a ListBox with groups

查看:27
本文介绍了WPF:将集合与集合绑定到具有组的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时 WPF 对我来说太复杂了.我的Window1"拥有一组Group".Group"是一个包含Person"集合的类.最后,这应该是一个联系人列表.我只想在 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.

这里的许多示例显示了只有一个集合的项目分组.我可以做的是将组名设置为Person"的属性.但后来我数不清(这确实是必要的):- 每组有多少人- 其中有多少人拥有状态"在线".

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:

假设你的课程是这样的:

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>

这篇关于WPF:将集合与集合绑定到具有组的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆