Search code examples
wpfvb.netxamlribbon

How do I get or set the selected index of a wpf ribbon combo box?


I have been looking all over the internet (googling pretty much) and have found no answer to this simple question and thus I'm starting to think it might not be possible.

Ok so I have a RibbonCombobox up in my ribbon. Here's the code

<ribbon:RibbonComboBox  
                  SelectionBoxWidth="150"
                  VerticalAlignment="Center" 
                  IsEditable="False" Name="cbConsultationRapideEmploye">
                        <ribbon:RibbonGallery Name="cbConsultationRapideEmployeG" SelectedValuePath="Name"
                          MaxColumnCount="1">
                            <ribbon:RibbonGalleryCategory Name="cbConsultationRapideEmployeGC" Margin="0" Padding="0" ItemsSource="{Binding}" DisplayMemberPath="NomEmploye" />
                        </ribbon:RibbonGallery>
                    </ribbon:RibbonComboBox>

I populate this combobox from a dataset on load.

What I want to do is set on load the selected item to be the first one in the list. I know that there is a SelectedItem property on the RibbonGallery, but sometimes it works just way better with an index.

I could probably roll with setting the SelectedItem as the first item in my Dataset but in the case of fetching info, fetching the text is not really an option as I will want the ID (from the database) of the employee and not his name.

If someone has an alternate solution I'm all open to suggestions.

EDIT: As per request here is the code where the combobox gets his values.

bd.openConnection()
dsCbNomEmploye = bd.queryds("SELECT Prenom + ' ' + Nom AS NomEmploye FROM tblEmploye ORDER BY 1", "tblEmploye")
cbConsultationRapideEmploye.DataContext = dsCbNomEmploye.Tables(0).DefaultView

Solution

  • I ended up adding the SelectedValuePath on the RibbonGallery tag like so

    <ribbon:RibbonComboBox  
                      SelectionBoxWidth="150"
                      VerticalAlignment="Center" 
                      IsEditable="False" Name="cbConsultationRapideEmploye">
                            <ribbon:RibbonGallery SelectedValuePath="idEmploye" Name="cbConsultationRapideEmployeG"
                              MaxColumnCount="1">
                                <ribbon:RibbonGalleryCategory Name="cbConsultationRapideEmployeGC" Margin="0" Padding="0" ItemsSource="{Binding}" DisplayMemberPath="NomEmploye" />
                            </ribbon:RibbonGallery>
                        </ribbon:RibbonComboBox>
    

    Afterwards if you need the text value you can get it with the cbConsultationRapideEmployeG.SelectedItem and if you need the actual value you can get it with cbConsultationRapideEmployeG.SelectedValue

    In short SelectedItem=Displayed value SelectedValue=not shown value