【问题标题】:How to use a RibbonGallery with a RibbonComboBox如何将 RibbonGallery 与 RibbonComboBox 一起使用
【发布时间】:2020-07-05 03:29:11
【问题描述】:

所以我想在 WPF 中的功能区中添加一个 RibbonCombobox。由于某种原因,RibbonCombobox 没有 selectionchanged 事件。我读到您应该使用 RibbonGallery 进行选择更改事件,所以我实现了这个

 <RibbonComboBox   Label="Equations" x:Name="EquationListComboToolbar"  ItemsSource="{Binding}">
                            <RibbonGallery x:Name="EquationListComboboxGallery" SelectedValue="{Binding  XPath=.}" />
                        </RibbonComboBox>

在幕后绑定是这样完成的。

  EquationListComboToolbar.DataContext = ViewModel.EquationNames;
                this.Bind(ViewModel, vm => vm.SelectedEquation, v => v.EquationListComboboxGallery.SelectedItem).DisposeWith(cleanup);
                Observable.FromEventPattern(EquationListComboboxGallery, nameof(EquationListComboboxGallery.SelectionChanged)).Subscribe(e => ViewModel.SelectEquation(EquationListComboboxGallery.SelectedItem?.ToString()));

在运行时出现以下错误

“在 WindowsBase.dll 中发生了“System.InvalidOperationException”类型的未处理异常 在使用 ItemsSource 之前,项目集合必须为空。”当应用程序初始化时。我知道这是关于画廊的问题,但我不知道是什么问题以及如何实现这一点。

按照我的建议,我已经尝试了建议的答案

 <RibbonComboBox   Label="Equations" x:Name="EquationListComboToolbar"  ItemsSource="{Binding}">
                            <RibbonComboBox.ItemTemplate>
                                <DataTemplate>
                                    <RibbonGallery x:Name="EquationListComboboxGallery" SelectedValue="{Binding  XPath=.}" />
                                </DataTemplate>
                            </RibbonComboBox.ItemTemplate>
                        </RibbonComboBox>

这样做会使得绑定不可能

【问题讨论】:

  • 您已将 RibbonGallery 添加为项目。
  • @Clemens 我尝试将其添加为项目模板,但随后 EquationListComboboxGallery 无法再绑定。
  • @Clemens 你能重新打开这个问题吗,你刚刚发布了一个已回答问题的链接,它根本没有回答我的问题。
  • 它确实告诉你你做错了什么。不要将 RibbonGallery 添加为项目,也不要绑定 ItemsSource 属性。如果您想知道您实际上应该做什么,请改写问题,尤其是标题。
  • @Clemens 不,它没有,那里的所有答案都有同样的问题 a missing parent 。基于这个答案stackoverflow.com/questions/14943134/… RibbonGallery 不需要父母。如果您不愿意提供帮助,请不要将问题关闭为重复。我一直在搜索关于此事的所有可能的帖子 3 小时。这并不是说我没有尽我所能。

标签: wpf ribbon reactiveui


【解决方案1】:

啊,是的。 Microsoft 功能区库非常有趣。幸运的是,我以前曾走过这条路。这是来自我的一个应用程序的RibbonComboBox 的工作示例,并带有RibbonGallery

<RibbonComboBox DropDownHeight="400">
    <RibbonGallery MaxColumnCount="1" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedItem="{Binding MySelectedItemProperty}">
        <RibbonGalleryCategory ItemsSource="{Binding MyItemsSourceProperty}"/>
    </RibbonGallery>
</RibbonComboBox>

我不完全确定这是做事的唯一方法,但我知道这种方法可行。请注意,我在RibbonGalleryCategory 上设置了ItemsSource,而不是RibbonComboBox 本身。可能在没有RibbonGalleryCategory 的情况下使用RibbonGallery,在这种情况下,您可以在RibbonGallery 上设置ItemsSource,但我没有对此进行测试。

请注意,您还可以将多个画廊类别添加到单个 RibbonComboBox,如下所示:

<RibbonComboBox DropDownHeight="400">
    <RibbonGallery MaxColumnCount="1" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedItem="{Binding MySelectedItemProperty}">
        <RibbonGalleryCategory ItemsSource="{Binding MyFirstItemsSourceProperty}"/>
        <Separator/>
        <RibbonGalleryCategory ItemsSource="{Binding MySecondItemsSourceProperty}"/>
    </RibbonGallery>
</RibbonComboBox>

以上内容可让您在同一个下拉列表中显示多个列表,并允许用户从任何列表中选择单个项目。像这样的功能可能是 RibbonGalleryCategory 存在的原因。

【讨论】:

  • 哦,谢谢你,我会尽快在功能上再次使用它,希望明天。但它看起来像我需要的。
  • 谢谢它的工作,奇怪的是你需要这种复杂的 XAML 来拥有一个包含动态项目和 SelectionChange 事件的组合框......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 2023-03-13
  • 2014-09-25
  • 2016-01-30
相关资源
最近更新 更多