GroupStyles 如何工作?

发布于 2024-11-09 13:30:59 字数 1266 浏览 0 评论 0原文

我有一个 ListView 控件绑定到 ViewModel 中的 ListCollectionView

我想尝试对这些项目进行分组,但遇到了一些问题。

我首先在虚拟机中设置属性分组,然后添加一个 GroupStyle

C#:

ListCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("Category"));

XAML:

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding Path=Name}"/>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListView.GroupStyle>

但是列表现在只是类别名称,无法查看项目本身。

我并不完全理解这里发生了什么。当我为 GroupStyle 创建模板时,我真正绑定的是什么?除了 Name 之外还有其他属性吗?

我刚刚将 GroupStyle 添加到我已经创建的 ListView 中,例如,我在其中包含了 ItemTemplate。这是否会扰乱 GroupStyle

如果列表中的项目属于另一个类,并且我不想根据它们所属的类实例(它有一个 ID)进行分组,该怎么办?然后,我会将组名称作为该父类的属性。这可能吗?

部分解决方案:

问题出在 ListView 上应用的样式。我不知道这种风格有什么关系。

完整解决方案

我没有在列表框中使用 ItemsPresenter ControlTemplate 选择使用将 IsItemsHost 设置为的面板。看来必须使用 ItemsPresenter 才能使 GroupStyling 正常工作。

I have a ListView Control bound to a ListCollectionView in a ViewModel.

I wanted to try to group these items but having some problems.

I set the Property grouping in the VM to begin with and then added a GroupStyle.

C#:

ListCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("Category"));

XAML:

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding Path=Name}"/>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListView.GroupStyle>

However the list is now just the category names, no way to see the items themselves.

I'm not really understanding completely what is going on here. When I create a Template for the GroupStyle what am I really binding to? Are there other properties besides Name ?

I just added the GroupStyle to a ListView I has already created where I for example included a ItemTemplate. Is that something that is messing with the GroupStyle?

What if the Items in the list belong to another class and I wan't to group based on what instance of class they belong to (it has an ID). I would then have the group name as a property on this parent class. Is that possible?

PARTIAL SOLUTION:

Problem was with the style applied on the ListView. I have no idea what about the style was interefering.

FULL SOLUTION

I wasn't using a ItemsPresenter in my listbox ControlTemplate opting to use a Panel with IsItemsHost set to true. It seems ItemsPresenter must be used for GroupStyling to work correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2024-11-16 13:30:59

我认为错误出在你的代码的其他地方。

通常,您在您的 ViewModel 上公开一组 Models

namespace Derp
{
    public sealed class ViewModel
    {
      public ObservableCollection<Model> Items {get;set;}
      // initialization code not shown
    }

    public sealed class Model
    {
      public string GroupName {get;set;}
      public string ModelName {get;set;}
    }
}

在您的视图中,您将 CollectionViewSource 绑定到此集合:

<Window.DataContext>
    <ViewModel xmlns="clr-namespace:Derp" />
</Window.DataContext>
<Window.Resources>
    <CollectionViewSource
        Source="{Binding Items}"
        x:Key="GroupedItems">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription
                PropertyName="GroupName" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

接下来,我们绑定我们的列表对此 CollectionViewSource 的控制(在本例中使用组合):

<ComboBox
    ItemsSource="{Binding Source={StaticResource GroupedItems}}"
    DisplayMemberPath="ModelName">
    <ComboBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock
                        Text="{Binding Name}" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ComboBox.GroupStyle>
</ComboBox>

可能会令人困惑的是,在 GroupStyle 中,您没有绑定到您的模型 ,您正在绑定一个模型集合,该集合按(在本例中)属性“GroupName”分组。 CollectionViewSource 将您的模型分组为扩展CollectionViewGroup。这些组有一个名为 Name 的属性,其中包含对模型进行分组的通用值(GroupName 属性的值)。因此,在 HeaderTemplate 中,您将绑定到 CollectionViewGroup.Name

I think the error lies elsewhere in your code.

Usually, you expose a collection of Models on your ViewModel

namespace Derp
{
    public sealed class ViewModel
    {
      public ObservableCollection<Model> Items {get;set;}
      // initialization code not shown
    }

    public sealed class Model
    {
      public string GroupName {get;set;}
      public string ModelName {get;set;}
    }
}

In your View, you bind a CollectionViewSource to this collection:

<Window.DataContext>
    <ViewModel xmlns="clr-namespace:Derp" />
</Window.DataContext>
<Window.Resources>
    <CollectionViewSource
        Source="{Binding Items}"
        x:Key="GroupedItems">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription
                PropertyName="GroupName" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

Next, we bind our list control to this CollectionViewSource (using a combo in this example):

<ComboBox
    ItemsSource="{Binding Source={StaticResource GroupedItems}}"
    DisplayMemberPath="ModelName">
    <ComboBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock
                        Text="{Binding Name}" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ComboBox.GroupStyle>
</ComboBox>

Where it can get confusing is that, within the GroupStyle, you aren't binding against your Model, you are binding against a collection of Models which is grouped on (in this case) the property "GroupName". The CollectionViewSource groups your Models into collections that extend CollectionViewGroup. These groups have a property called Name, which contains the common value on which your Models are grouped (the value of the GroupName property). So, in the HeaderTemplate, you are binding to CollectionViewGroup.Name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文