Search code examples
wpfresourcesribbon

Microsoft WPF ribbon - how can I add a RibbonTab stored in a Resources.xaml file?


I have a RibbonTab defined in a Resource file (xaml resources) and i need to add it to the ribbon's tabs collection.

How do i do that? (In xaml)

The Ribbon in xaml is something like that:

<Ribbon>
   <Ribbon.Tabs><Ribbon.Tabs/>
</Ribbon>

So it holds a collection of tabs, i don't know how to insert a tab stored in the static resources.

Thank you in advance :) Teodor

edit: This is the WPF Microsoft ribbon

Edit 2: I tried using <DynamicResource ResourceKey="MyTabKey" /> but i get this error:

Property 'Tabs' does not support values of type 'DynamicResourceExtension'


Solution

  • First you place the RibbonTabs as resources in whatever ResourceDictionary you want:

    <Application.Resources>
            <r:RibbonTab Label="Tab_A" x:Key="RibControl_A">
                <r:RibbonGroup>
                    <r:RibbonButton>
                        <r:RibbonButton.Command>
                            <r:RibbonCommand LabelTitle="CommandA"/>
                        </r:RibbonButton.Command>
                    </r:RibbonButton>
                </r:RibbonGroup>
            </r:RibbonTab>
            <r:RibbonTab Label="Tab_B" x:Key="RibControl_B">
                <r:RibbonGroup>
                    <r:RibbonButton>
                        <r:RibbonButton.Command>
                            <r:RibbonCommand LabelTitle="CommandB"/>
                        </r:RibbonButton.Command>
                    </r:RibbonButton>
                </r:RibbonGroup>
            </r:RibbonTab> 
    </Application.Resources>
    

    Then you can just reference them as StaticResources

    <r:Ribbon>
            <r:Ribbon.Tabs>
                <StaticResource ResourceKey="RibControl_A" />
                <StaticResource ResourceKey="RibControl_B" />
            </r:Ribbon.Tabs>
    </r:Ribbon>
    

    That compiles and runs successfully for me.