1

So I wanted to add a RibbonCombobox to my Ribbon in WPF. For some reason, RibbonCombobox does not have a selectionchanged event. I read that you should use a RibbonGallery for selection change event so I implemented this

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

Behind the scene the binding is done like this.

  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()));

At runtime I get the following error

"An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll Items collection must be empty before using ItemsSource." When the app initalizez. I know it's something about the Gallery but I can't figure out what is the problem and how can I achieve this.

As I was suggested, I already tried the answer that was suggested

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

Doing this, will make by binding imposible

enter image description here

Clemens
  • 123,504
  • 12
  • 155
  • 268
CiucaS
  • 2,010
  • 5
  • 36
  • 63
  • You have added a RibbonGallery as item. – Clemens Mar 24 '20 at 15:47
  • @Clemens I tried to add it as an item template but then EquationListComboboxGallery it's no more available to bind behind. – CiucaS Mar 24 '20 at 15:54
  • @Clemens can you please reopen the question, you just posted a link to an answered question, which does not answer my question at all. – CiucaS Mar 24 '20 at 16:08
  • It does exactly tell you what you did wrong. Do not add a RibbonGallery as item, or do not bind the ItemsSource property. If you want to know what you should actually do, rephrase the question and especially the title. – Clemens Mar 24 '20 at 16:09
  • @Clemens no it does not, all the answers there have the same problem a missing parent . Based on this answer https://stackoverflow.com/questions/14943134/ribboncombobox-has-no-selectionchanged-event RibbonGallery does not require a parent. If you are not willing to help, don;'t just close the question as duplicate. I'm been search for 3 hours on all the posts possible about this matter. It's not like I did not try everything I possible could. – CiucaS Mar 24 '20 at 16:13
  • Then edit the question title. The cause of the error message is obvious. What you actually want to know is something completely different. – Clemens Mar 24 '20 at 16:15
  • I honestly have no idea what else to put in the title, feel free to edit it, as it looks like you know better. – CiucaS Mar 24 '20 at 16:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210245/discussion-between-ciucas-and-clemens). – CiucaS Mar 24 '20 at 16:18
  • From your very first example, remove the ItemsSource from the RibbonCombobox completely. – Lee McPherson Mar 24 '20 at 17:17
  • That will not show any elements in my combobox, why would I do that that? Just to have a working ribboncombobox with no items. – CiucaS Mar 25 '20 at 10:36

1 Answers1

3

Ah, yes. The Microsoft ribbon library is lots of fun. Luckily I've been down this road before. Here's a working example of a RibbonComboBox from one of my applications, complete with RibbonGallery:

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

I'm not entirely sure this is the only way to do things, but I know this way works. Note that I set ItemsSource on the RibbonGalleryCategory, not the RibbonComboBox itself. It might be possible to use the RibbonGallery without a RibbonGalleryCategory, in which case you would set ItemsSource on RibbonGallery, but I've not tested this.

Note you also have the ability to add multiple galleries categories to a single RibbonComboBox like so:

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

The above lets you show multiple lists in the same drop down and allows the user to select a single item from any list. Functionality like this is probably why RibbonGalleryCategory exists in the first place.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
  • Oh thank you, I will try it as soon as I get to that functionally again, hopefully tomorrow. But it looks like what I need. – CiucaS Mar 26 '20 at 08:31
  • Thank you it works, it is strange that you need this kind of a complex XAML to have a combobox that have dynamic items and and SelectionChange event... – CiucaS Mar 27 '20 at 12:28
  • To display a default value for the selected item binding property, add `IsSynchronizedWithCurrentItem="True"` on the `RibbonGallery` (as explained here: https://stackoverflow.com/a/43142542/3809520) – royalTS Oct 13 '22 at 08:11