将 IGrouping 绑定到功能区组
我使用 WPF 功能区控件并取得了一些成功;我现在尝试使用功能区库,在数据绑定场景中使用类别。以下是一些示例数据: -
var data = new[]
{
new { Category = "Sport", Hobby = "Football" },
new { Category = "Sport", Hobby = "Table Tennis" },
new { Category = "Music", Hobby = "Guitar" },
new { Category = "Music", Hobby = "Piano" },
new { Category = "PC", Hobby = "StarCraft 2" },
};
我正在对数据进行分组,并希望在图库中显示按类别分组的项目: -
IEnumerable CategorisedHobbies;
CategorisedHobbies = data.GroupBy(d => d.Category).ToArray();
全部相当标准。我的 XAML 如下所示: -
<ribbon:RibbonGallery ItemsSource="{Binding CategorisedHobbies}">
<ribbon:RibbonGallery.ItemTemplate>
<DataTemplate>
<ribbon:RibbonGalleryCategory Header="{Binding Key}" ItemsSource="{Binding}" MaxColumnCount="1">
<ribbon:RibbonGalleryCategory.ItemTemplate>
<DataTemplate>
<ribbon:RibbonGalleryItem Content="{Binding Hobby}"/>
</DataTemplate>
</ribbon:RibbonGalleryCategory.ItemTemplate>
</ribbon:RibbonGalleryCategory>
</DataTemplate>
</ribbon:RibbonGallery.ItemTemplate>
</ribbon:RibbonGallery>
但是,当应用程序运行时,虽然我正确地获取了功能区库中显示的类别,但每个项目只是一个空白方块。我知道这些系列正在被绑定,因为我可以看到例如运动类的类别大小比 PC 类的大小更大。
如果我按如下方式对 XAML 进行硬编码,那么当然一切正常:-
你知道我在这里做错了什么吗?谢谢!
I'm using the WPF ribbon control with some success; I'm trying now to use the Ribbon Gallery, making use of the Categories in a data-bound scenario. Here's some example data: -
var data = new[]
{
new { Category = "Sport", Hobby = "Football" },
new { Category = "Sport", Hobby = "Table Tennis" },
new { Category = "Music", Hobby = "Guitar" },
new { Category = "Music", Hobby = "Piano" },
new { Category = "PC", Hobby = "StarCraft 2" },
};
I'm grouping up the data and want to display the items in a gallery, grouped by Category: -
IEnumerable CategorisedHobbies;
CategorisedHobbies = data.GroupBy(d => d.Category).ToArray();
All fairly standard. My XAML looks as follows: -
<ribbon:RibbonGallery ItemsSource="{Binding CategorisedHobbies}">
<ribbon:RibbonGallery.ItemTemplate>
<DataTemplate>
<ribbon:RibbonGalleryCategory Header="{Binding Key}" ItemsSource="{Binding}" MaxColumnCount="1">
<ribbon:RibbonGalleryCategory.ItemTemplate>
<DataTemplate>
<ribbon:RibbonGalleryItem Content="{Binding Hobby}"/>
</DataTemplate>
</ribbon:RibbonGalleryCategory.ItemTemplate>
</ribbon:RibbonGalleryCategory>
</DataTemplate>
</ribbon:RibbonGallery.ItemTemplate>
</ribbon:RibbonGallery>
However, when the app runs, whilst I correctly get the categories showing in the ribbon gallery, each item is just a blank square. I know that the collections are getting bound because I can see that the category size is bigger for e.g. Sport than PC.
If I hard-code the XAML as follows it of course all works: -
Any ideas what I'm doing wrong here? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我现在已经“正常”工作了。我要做的不是设置 DataTemplate,而是为 RibbonGallery 上的 ItemsContainerStyle 应用样式。
此样式只需为 RibbonGalleryCategory 类型,并且具有 ItemsSource 的属性设置器。就我而言,它只是 {Binding},另外我还必须设置 DisplayMemberPath。
我仍然没有完全理解 RibbonGallery 的层次结构以及它如何设计事物 - 但至少这种方法是有效的。
更新:
以下是我最初提供的代码示例的相应 XAML:
OK, I have gotten this working now "properly". What I had to do was rather than set the DataTemplate was to apply a style for the ItemsContainerStyle on the RibbonGallery.
This style simply needs to be of type RibbonGalleryCategory, and to have a property setter for the ItemsSource. In my case, it was simply {Binding}, plus I had to set the DisplayMemberPath.
I still don't have a full understanding of the hierarchy of the RibbonGallery in terms of how it styles things - but at least this approach works.
UPDATE:
Here's the appropriate XAML for the code example I originally supplied:
不知道为什么,但如果您将
ItemsPanel
分配给RibbonGalleryCategory
,它会起作用:Not sure why but if you assign an
ItemsPanel
toRibbonGalleryCategory
, it works: