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

    Hi,
    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.
    Thanks.
  2. Hristo
    Admin
    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" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
        Width="400" Height="300">  
        <Grid x:Name="LayoutRoot" Background="White">  
            <Button Content="Change my content with ContextMenu" x:Name="btn1" 
                    VerticalAlignment="Center" /> 
        </Grid> 
    </UserControl> 

    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()  
            {  
                InitializeComponent();  
                RadContextMenu buttonContextMenu = new RadContextMenu();  
                buttonContextMenu.EventName = "Click";  
                RadMenuItem item1 = new RadMenuItem()  
                {  
                    Header = "Change Content" 
                };  
                RadMenuItem item11 = new RadMenuItem()  
                {  
                    Header = "To String" 
                };  
                item1.Items.Add(item11);  
                RadMenuItem item12 = new RadMenuItem()  
                {  
                    Header = "To Integer" 
                };  
                item1.Items.Add(item12);  
                buttonContextMenu.Items.Add(item1);  
     
                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,
    Hristo
    the Telerik team

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