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

Add Context menu in run time(dynamicly)

1 Answer 183 Views
Menu
This is a migrated thread and some comments may be shown as answers.
ruty
Top achievements
Rank 1
ruty asked on 26 Mar 2009, 09:00 AM
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.

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 26 Mar 2009, 09:58 AM
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.
Tags
Menu
Asked by
ruty
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or