【发布时间】: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