Click here to Skip to main content
15,910,358 members
Articles / Desktop Programming / WPF

Ribbon (WPF) – ribbon:RibbonComboBox

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
16 Sep 2011CPOL 34.3K   1   6
Ribbon (WPF): ribbon:RibbonComboBox

How to 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

XML
<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 be included directly into the XAML code as ItemSource = {x:Static Fonts.SystemFontFamilies}

Finally, some useful links:

This article was originally posted at http://blog.kribo.be/blog?p=180

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Belgium Belgium
Developer within C#, Dynamics NAV (Navision), Php environments.

Comments and Discussions

 
GeneralGreat help for dynamic databinding Pin
MCY7-Feb-14 1:49
professionalMCY7-Feb-14 1:49 
QuestionWhat would it look like using MVVM? Pin
dietmar paul schoder25-Nov-13 5:44
professionaldietmar paul schoder25-Nov-13 5:44 
AnswerRe: What would it look like using MVVM? Pin
dietmar paul schoder25-Nov-13 5:59
professionaldietmar paul schoder25-Nov-13 5:59 
GeneralRe: What would it look like using MVVM? Pin
EngDRJ17-Mar-15 12:03
professionalEngDRJ17-Mar-15 12:03 
QuestionCombobox item not getting selected Pin
bizzare19883-Nov-11 19:18
bizzare19883-Nov-11 19:18 
AnswerRe: Combobox item not getting selected Pin
kribo3-Nov-11 21:27
professionalkribo3-Nov-11 21:27 
Hi,

After a quick look at your code I discovered that you're newWindow is already open before assigning the comboBox
C#
newWindow = new WpfApplication4.Window2();
newWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
newWindow.Owner = this;
newWindow.Show();

RibbonGallery source = e.OriginalSource as RibbonGallery;
if (source == null) return;
var selected=source.Items.CurrentItem;
newWindow.combobox_selectedfile = selected.ToString();


So a small tip a little rule of thumb....
Always handle the incoming of the Methode before the rest and then break it down into small steps.
Thus making it easier to find where it goes wrong..
Once you're shore it does all intended you revise your code...

So I wrote a little sample code, but I haven't had the time to test it.
I wrote it as I was writing this reply, so appologies if it doesn't work straight away.

1- First of all I always test for a "source" before running any code.
Why, if there is nothing don't bother creating a new window instance.

2- Run a simple test for ease of mind

3- Then retreave the selectedItem = currentItem

4- Again run a test for ease of mind

5- If selectedItem exists then create a new window instance

C#
private void RibbonGallery_SelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    //First test if source exists
    RibbonGallery source = e.OriginalSource as RibbonGallery;
    if (source == null) return;

    //Testing
    MessageBox.Show(source.Name.ToString());

    //difine the next peace of code only to the comboBox(happyFeet)
    if(source.Name == "happyFeet")
    {

        //Retreaving the selectedItem
        var selected = source.SelectedItem; 
    
        //Testing 
        MessageBox.Show(selected.ToString());
    
        //The new window
        if(selected == null)
        {
         return;
        }
        else
        {
           newWindow = new WpfApplication4.Window2();
           newWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
           newWindow.Owner = this;
           newWindow.combobox_selectedfile = selected.ToString();
           newWindow.Show();
        }
        
        // You coluld also use a (try - catch) instead of the if
    }

}


I hope this answers your question and helps you.

Don't get discouraged, have fun and keep on coding dude......
Kribo

htt://blog.kribo.be

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.