所以我想在WPF中将RibbonCombobox添加到我的Ribbon中。由于某些原因,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之前,Items集合必须为空。”当应用程序启动时,我知道这是关于Gallery的,但是我无法弄清楚问题出在哪里以及如何实现。
根据我的建议,我已经尝试了所建议的答案
<RibbonComboBox Label="Equations" x:Name="EquationListComboToolbar" ItemsSource="{Binding}">
<RibbonComboBox.ItemTemplate>
<DataTemplate>
<RibbonGallery x:Name="EquationListComboboxGallery" SelectedValue="{Binding XPath=.}" />
</DataTemplate>
</RibbonComboBox.ItemTemplate>
</RibbonComboBox>
这样做,将无法绑定
啊,是的。 Microsoft功能区库很有趣。幸运的是我以前走过这条路。这是我的一个应用程序中的RibbonComboBox
的有效示例,并带有RibbonGallery
:
<RibbonComboBox DropDownHeight="400">
<RibbonGallery MaxColumnCount="1" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedItem="{Binding MySelectedItemProperty}">
<RibbonGalleryCategory ItemsSource="{Binding MyItemsSourceProperty}"/>
</RibbonGallery>
</RibbonComboBox>
我不完全确定这是做事的唯一方法,但我知道这种方法有效。请注意,我将ItemsSource
设置为RibbonGalleryCategory
,而不是RibbonComboBox
本身。可能可以不使用RibbonGallery
而使用RibbonGalleryCategory
,在这种情况下,可以将ItemsSource
设置为RibbonGallery
,但我尚未对此进行测试。
注意,您还可以将多个图库类别添加到单个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
首先存在的原因。