Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Ribbon (WPF) - RibbonComboBox

0.00/5 (No votes)
11 Sep 2011 1  
Ribbon (WPF) - ribbon:RibbonComboBox
Howto use the ribbon:RibbonCombobox

The main question is how do I retrieve the selectedItem from the ribbonComboBox.
The answer is in the "naming" of the XAML elements and using the correct element-properties.

Step 1) Mainwindow.xaml
XAML
<wrappanel>
  <ribbon:ribboncombobox selectionboxwidth="100">
     <ribbon:ribbongallery x:name="fontsComboBox">
                SelectionChanged="RibbonGallery_SelectionChanged">
        <ribbon:ribbongallerycategory x:name="fonts" />
     </ribbon:ribbongallery>
  </ribbon:ribboncombobox>

  <ribbon:ribboncombobox selectionboxwidth="40" >
      <ribbon:ribbongallery x:name="fontSizeComboBox">
               SelectionChanged="RibbonGallery_SelectionChanged">
         <ribbon:ribbongallerycategory x:name="fontSize" />
      </ribbon:ribbongallery>
   </ribbon:ribboncombobox>
</wrappanel>


Step 2) Mainwindow.xaml.cs
C#
public MainWindow()
{
    InitializeComponent();
    InitializeFonts();
}
public void InitializeFonts()
{
    fonts.ItemsSource = Fonts.SystemFontFamilies;
    fontsComboBox.SelectedItem = "Arial";

    for (double i = 8; i < 48; i += 2)
    {
         fontSize.Items.Add(i);
     }
     fontSizeComboBox.SelectedItem = "8";
}


Step 3) The SelectionChanged
C#
private void RibbonGallery_SelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    RibbonGallery source = e.OriginalSource as RibbonGallery;
    if (source == null) return;
    switch (source.Name)
    {
       case "fontsComboBox":
         //change the font face
         _documentManager.ApplyToSelection(
                               TextBlock.FontFamilyProperty, 
                               source.SelectedItem);
          break;
       case "fontSizeComboBox":
        //Change the font size
         _documentManager.ApplyToSelection(
                              TextBlock.FontSizeProperty, 
                              source.SelectedItem);
         break;
    }
}

In short, the RibbonGallery contains the SelectionChanged method and the RibbonGalleryCategory contain the ItemSource.

fonts.ItemsSource = Fonts.SystemFontFailies can also be included directly into the XAML code as ItemSource = {x:Static Fonts.SystemFontFamilies}

Finally, some useful links:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here