This is a migrated thread and some comments may be shown as answers.

Different Context Menus for Different Tree View Nodes

3 Answers 193 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
JDT
Top achievements
Rank 1
JDT asked on 06 Jun 2012, 07:35 AM
Hi Telerik,

Is there a way I can define 4 different context menus in XAML and then programatically assign them as needed to be the one shown when clicking nodes?

so once the context menus are defined in xaml, they would be swapped out programatically with one line of code. treeView.ContextMenu = menu1, menu2, etc

Although I can generate all context menus programatically, prefer to have them in xaml to easily modify and make design changes. I tried adding multiple context menus but cannot. Is there a way to define xaml objects somewhere else and then assign them as needed?

For example (one radtreeview with 2 root top level nodes)
(when clicking customers context menu shows custom menu for cutomers with items like (send customer invoice, send letter)
-Customers
    +Lockhead 
    +Raytheon

(when clicking internal staff context menu shows custom menu for staff with items like (call customer, email customer)
-Internal Staff
    +Marketing Director
    +Sales Director

3 Answers, 1 is accepted

Sort by
0
Lancelot
Top achievements
Rank 1
answered on 06 Jun 2012, 04:22 PM
Hi JDT,

I would create several ContextMenuStyles in your <UserControl.Resources>, therefore you can use the style depending on what you want to use. As long as you name each style differently by Key propety value, you'll be able to call on the style in your context menu.

This documentation will show you the visual structure of the RadContextMenu template. Once you've created a couple different styles in your user control's resources, you can assign it by the template's key name (ex. Key="IndividualStyleName"  would be called by using Style="{StaticResource IndividualStyleName}"   )

Let me know if this doesn't satify your needs and we can dig deeper into your code. Provide an example of your code and we can take it from there.

Good Luck,
Lancelot
0
JDT
Top achievements
Rank 1
answered on 06 Jun 2012, 06:56 PM
Lancelot,

Thanks for the reply. How would you go about using a style to set event handlers and adding items? An example would be great. I would be swapping in and out between these 2 context menus for example.


<telerikNavigation:RadContextMenu.ContextMenu>
            <telerikNavigation:RadContextMenu x:Name="contextMenuCustomers" IconColumnWidth="0"  Opened="contextMenuCustomers_Opened">
                <telerikNavigation:RadMenuItem x:Name="contextMenuCustomers_SendLetter" Header="Send Letter" Click="contextMenuCustomers_SendLetter"/>
                <telerikNavigation:RadMenuItem IsSeparator="True" />
                <telerikNavigation:RadMenuItem x:Name="contextMenuCustomers_SendInvoice" Header="Send Invoice" Click="contextMenuCustomers_SendInvoice"/>
                <telerikNavigation:RadContextMenu>
            </telerikNavigation:RadContextMenu.ContextMenu>
             
            <telerikNavigation:RadContextMenu.ContextMenu>
                <telerikNavigation:RadContextMenu x:Name="contextMenuStaff" IconColumnWidth="0"  Opened="contextMenuStaff_Opened">
                <telerikNavigation:RadMenuItem x:Name="contextMenuStaff_CallCustomer" Header="Call Customer" Click="contextMenuStaff_CallCustomer"/>
                <telerikNavigation:RadMenuItem IsSeparator="True" />
                <telerikNavigation:RadMenuItem x:Name="contextMenuStaff_EmailCustomers" Header="Email Customer" Click="contextMenuStaff_EmailCustomer"/>
            <telerikNavigation:RadContextMenu>
</telerikNavigation:RadContextMenu.ContextMenu>
0
Lancelot
Top achievements
Rank 1
answered on 06 Jun 2012, 07:28 PM
JDT,

This link will show you how to style the RadContextMenu. In your case you said you needed four of them, so you'll be creating four separate styles. It important you create these styles before going onto the next step, you could just build two to start with. My example below will give you the idea of how to use a style.

Then depending on where you want to use it, you override the default style by writing in Style = "{StaticResource YourStyleHere}"  inside your context menu directly. You keep them the way you already have them written, each with their own event handlers. They style doesn't effect the menuitems, just the context menu itself. Your events take place on the MenuItem click. " Using your example below..

telerikNavigation:RadContextMenu x:Name="contextMenuStaff" IconColumnWidth="0" Opened="contextMenuStaff_Opened" Style = "{StaticResource YourFirstStyleHere}"  >

<telerikNavigation:RadContextMenu x:Name="contextMenuCustomers" IconColumnWidth="0" Opened="contextMenuCustomers_Opened"Style = "{StaticResource YourSecondStyleHere}"  >


So basically you'll have your separate styles in your resources section, but depending on what style you want in which context menu is up to you. It doesnt need to be programmatically changed, you've already predefinted it in your xaml.  This also allows you to make a small edit to the style and all the places in your app that use the style will reflect the change.

Good Luck
Lancelot
Tags
TreeView
Asked by
JDT
Top achievements
Rank 1
Answers by
Lancelot
Top achievements
Rank 1
JDT
Top achievements
Rank 1
Share this question
or