Add Context menu in run time(dynamicly)

2 posts, 0 answers
  1. ruty
    ruty avatar
    42 posts
    Member since:
    Mar 2009

    Posted 26 Mar 2009 Link to this post

    How I added context menu in run time??????(in cs file)
    Can you give me example please?????????
    (I want to addded context menu to text block or to empty area.
  2. Hristo
    Hristo avatar
    832 posts

    Posted 26 Mar 2009 Link to this post

    Hi ruty,

    Here is an example how to create RadContextMenu programmatically.
    This is the page.xaml
    <UserControl x:Class="SilverlightDockingDemo.Page56" 
        Width="400" Height="300">  
        <Grid x:Name="LayoutRoot" Background="White">  
            <Button Content="Change my content with ContextMenu" x:Name="btn1" 
                    VerticalAlignment="Center" /> 

    and this is the page code-behind:
    using System;  
    using System.Windows;  
    using System.Windows.Controls;  
    using Telerik.Windows;  
    using Telerik.Windows.Controls;  
    namespace SilverlightDockingDemo  
        public partial class Page56 : UserControl  
            public Page56()  
                RadContextMenu buttonContextMenu = new RadContextMenu();  
                buttonContextMenu.EventName = "Click";  
                RadMenuItem item1 = new RadMenuItem()  
                    Header = "Change Content" 
                RadMenuItem item11 = new RadMenuItem()  
                    Header = "To String" 
                RadMenuItem item12 = new RadMenuItem()  
                    Header = "To Integer" 
                buttonContextMenu.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));  
                RadContextMenu.SetContextMenu(btn1, buttonContextMenu);  
            private void OnMenuItemClick(object sender, RoutedEventArgs e)  
                RadRoutedEventArgs args = e as RadRoutedEventArgs;  
                RadMenuItem item = args.OriginalSource as RadMenuItem;  
                string header = Convert.ToString(item.Header);  
                if (header == "To String")  
                    btn1.Content = "String Content";  
                else if (header == "To Integer")  
                    btn1.Content = 42;  

    Let us know if you need more help.

    All the best,
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Back to Top