Different Context Menus for Different Tree View Nodes

4 posts, 0 answers
  1. JDT
    JDT avatar
    45 posts
    Member since:
    Jul 2007

    Posted 06 Jun 2012 Link to this post

    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

  2. Lancelot
    Lancelot avatar
    251 posts
    Member since:
    Jul 2012

    Posted 06 Jun 2012 Link to this post

    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
  3. DevCraft banner
  4. JDT
    JDT avatar
    45 posts
    Member since:
    Jul 2007

    Posted 06 Jun 2012 Link to this post

    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>
  5. Lancelot
    Lancelot avatar
    251 posts
    Member since:
    Jul 2012

    Posted 06 Jun 2012 Link to this post

    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
Back to Top