wpf 列表框分组问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21835902/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:54:07  来源:igfitidea点击:

ListBox Grouping issue

wpflistboxitemscontrol

提问by Lucifer

I am trying to Group my collection which is based on my following model:

我正在尝试根据我的以下模型对我的收藏进行分组:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Role PersonRole { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public string RoleName { get; set; }
}

My PersonCollection,

我的个人收藏,

public ObservableCollection<Person> PersonList;

The collection is populated with three Person object, two with identical roles.

该集合由三个 Person 对象填充,其中两个具有相同的角色。

I want to group all my persons as per their RoleName. I have the following XAML:

我想根据他们的角色名称对我所有的人进行分组。我有以下 XAML:

<Grid.Resources>

        <CollectionViewSource x:Key="cvs" Source="{Binding PersonList}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="PersonRole.RoleName"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

    </Grid.Resources>

<ListBox ItemsSource="{Binding Source={StaticResource cvs}}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding FirstName}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding PersonRole.RoleName}" FontWeight="Bold" Background="ForestGreen" Margin="0,5,0,0"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListBox.GroupStyle>
    </ListBox>

However the rendered UI is not displaying the grouped RoleName in the list box. Also I would like to show the grouping horizontally where the groups appear horizontally while the grouped items appear vertically. Appreciate any help in advance.

但是,呈现的 UI 不会在列表框中显示分组的 RoleName。此外,我想水平显示分组,其中组水平显示,而分组项目垂直显示。提前感谢任何帮助。

Here's the output I am seeing: enter image description here

这是我看到的输出: 在此处输入图片说明

回答by Rohit Vats

Since you have already provided GroupDescriptonsat your CollectionViewSource, groupStyle will pick the property name from there only.

由于您已经GroupDescriptons在 CollectionViewSource 中提供了,groupStyle 将仅从那里选择属性名称。

All you need is to bind with Nameproperty of CollectionViewGroupclassto access the value of the property on which grouping is applied. In group style, binding is against CollectionViewGroup class and not against your Model class.

您只需要绑定类的Name属性CollectionViewGroup即可访问应用分组的属性的值。在组样式中,绑定是针对 CollectionViewGroup 类而不是针对您的 Model 类。

So, this is how you will do it:

所以,这就是你将如何做到的:

<TextBlock Text="{Binding Name}" FontWeight="Bold"
           Background="ForestGreen" Margin="0,5,0,0"/>

And regarding to align groups horizontally, you can set ItemsPanelTemplateof Group to be VirtualizingStackPanelwith Orientationset to Horizontalwhich makes your groups to be aligned horizontally.

而对于要对齐组水平,你可以设置ItemsPanelTemplate集团将VirtualizingStackPanelOrientation设置为Horizontal使您的群体进行水平对齐。

<ListBox.GroupStyle>
   <GroupStyle>
      <GroupStyle.Panel>
         <ItemsPanelTemplate>
           <VirtualizingStackPanel Orientation="Horizontal"/>
         </ItemsPanelTemplate>
      </GroupStyle.Panel>
      <GroupStyle.HeaderTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding Name}" FontWeight="Bold"
                      Background="ForestGreen" Margin="0,5,0,0"/>
         </DataTemplate>
      </GroupStyle.HeaderTemplate>
   </GroupStyle>
</ListBox.GroupStyle>