简体   繁体   中英

Populating RibbonComboBox

I am learning WPF and have spent waaaay too much time trying to solve this simple task.

My goal is to add two RibbonComboBox es to a ribbon control. One for Categories , and another for Subcategories . Whenever the user changes the Category , I would like to have the Subcategories combo box update to show the subcategories for the current category.

XAML:

<RibbonTab Header="Home">
    <RibbonGroup Header="Category">
        <RibbonComboBox Label="Category:" HorizontalContentAlignment="Left" SelectionBoxWidth="250" Focusable="False">
            <RibbonGallery Name="galCategory" DisplayMemberPath="Text" SelectedValuePath="Value">
            </RibbonGallery>
        </RibbonComboBox>
        <RibbonComboBox Label="Subcategory:" HorizontalContentAlignment="Left" SelectionBoxWidth="250">
            <RibbonGallery Name="galSubcategory" DisplayMemberPath="Text" SelectedValuePath="Value">
            </RibbonGallery>
        </RibbonComboBox>
    </RibbonGroup>
</RibbonTab>

I found that only by adding the RibbonGallery element, I can access methods that allow me to populate the combo box. However, while my data shows in the list, the items cannot be selected by the user. (Clicking items in the list has no effect whatsoever.)

Can anyone tell me how to populate these controls?

Note: Bonus points to anyone who can tell me how to make the two combo boxes align up to each other regardless of the length of text in the label!

Add a RibbonGalleryCategory control inside RibbonGallery and then populate it. Add a selection change event listener to RibbonGallery :

XAML:

<RibbonComboBox Label="Category:" Name="rcmbCategory" SelectionBoxWidth="100" Height="20" HorizontalContentAlignment="Center">
    <RibbonGallery Name="galCategory" DisplayMemberPath="Text" SelectedValuePath="Value" SelectionChanged="RibbonGallery_SelectionChanged">
        <RibbonGalleryCategory Name="rgcCategory"/>
    </RibbonGallery>
</RibbonComboBox>

.CS:

public MainWindow()
{
    InitializeComponent();
    for(int i=0;i<=10;i++)
        rgcCategory.Items.Add(i);
}

private void RibbonGallery_SelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    MessageBox.Show(rcmbCategory.SelectionBoxItem.ToString());
}

Refer to CodeProject: RibbonComboBox for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM