GroupStyles 如何工作?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为错误出在你的代码的其他地方。
通常,您在您的 ViewModel 上公开一组 Models
在您的视图中,您将 CollectionViewSource 绑定到此集合:
接下来,我们绑定我们的列表对此 CollectionViewSource 的控制(在本例中使用组合):
可能会令人困惑的是,在 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
In your View, you bind a CollectionViewSource to this collection:
Next, we bind our list control to this CollectionViewSource (using a combo in this example):
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 theGroupName
property). So, in the HeaderTemplate, you are binding to CollectionViewGroup.Name.