将 IGrouping 绑定到功能区组

发布于 2024-10-08 20:27:21 字数 1858 浏览 0 评论 0原文

我使用 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 类的大小更大。

alt text

如果我按如下方式对 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.

alt text

If I hard-code the XAML as follows it of course all works: -

Any ideas what I'm doing wrong here? Thanks!

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

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

发布评论

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

评论(2

朮生 2024-10-15 20:27:21

好的,我现在已经“正常”工作了。我要做的不是设置 DataTemplate,而是为 RibbonGallery 上的 ItemsContainerStyle 应用样式。

此样式只需为 RibbonGalleryCategory 类型,并且具有 ItemsSource 的属性设置器。就我而言,它只是 {Binding},另外我还必须设置 DisplayMemberPath。

我仍然没有完全理解 RibbonGallery 的层次结构以及它如何设计事物 - 但至少这种方法是有效的。

更新:
以下是我最初提供的代码示例的相应 XAML:

<r:RibbonWindow.Resources>
    <Style TargetType="r:RibbonGalleryCategory" x:Key="HobbyCategoryStyle">
        <Setter Property="Header" Value="{Binding Key}"/>
        <Setter Property="ItemsSource" Value="{Binding}"/>
        <Setter Property="DisplayMemberPath" Value="Hobby"/>
    </Style>
</r:RibbonWindow.Resources>
<r:RibbonMenuButton Label="Example menu button">
   <r:RibbonGallery ItemsSource="{Binding CategorisedHobbies}" ItemContainerStyle="{StaticResource ResourceKey=HobbyCategoryStyle}"/>
</r:RibbonMenuButton>

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:

<r:RibbonWindow.Resources>
    <Style TargetType="r:RibbonGalleryCategory" x:Key="HobbyCategoryStyle">
        <Setter Property="Header" Value="{Binding Key}"/>
        <Setter Property="ItemsSource" Value="{Binding}"/>
        <Setter Property="DisplayMemberPath" Value="Hobby"/>
    </Style>
</r:RibbonWindow.Resources>
<r:RibbonMenuButton Label="Example menu button">
   <r:RibbonGallery ItemsSource="{Binding CategorisedHobbies}" ItemContainerStyle="{StaticResource ResourceKey=HobbyCategoryStyle}"/>
</r:RibbonMenuButton>
甚是思念 2024-10-15 20:27:21

不知道为什么,但如果您将 ItemsPanel 分配给 RibbonGalleryCategory,它会起作用:

<ribbon:RibbonGalleryCategory.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel IsItemsHost="True" />
    </ItemsPanelTemplate>
</ribbon:RibbonGalleryCategory.ItemsPanel>

Not sure why but if you assign an ItemsPanel to RibbonGalleryCategory, it works:

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