Icons in contaxMenu

6 posts, 0 answers
  1. runes
    runes avatar
    15 posts
    Member since:
    Mar 2009

    Posted 24 Mar 2009 Link to this post

    Hi,
    I want to add icons in contaxMenu dinamicly.(in run time).
    How I do it?
    Thanks.
  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 24 Mar 2009 Link to this post

    Hi runes,

    Here is an example how to change RadMenuItem Icons in code:
    This is the page.xaml

    <UserControl x:Class="SilverlightApplication26.Page" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:nav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
            Width="400" Height="300">  
        <StackPanel x:Name="LayoutRoot" Background="White">  
            <RadioButton Content="Click Me to Open the RadContextMenu">  
                <nav:RadContextMenu.ContextMenu> 
                    <nav:RadContextMenu EventName="Click" x:Name="radContextMenu1">  
                        <nav:RadMenuItem Header="Item 1" /> 
                        <nav:RadMenuItem Header="Item 2" /> 
                    </nav:RadContextMenu> 
                </nav:RadContextMenu.ContextMenu> 
            </RadioButton> 
        </StackPanel> 
    </UserControl> 

    and this is the code-behind:

    using System;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Media.Imaging;  
    using Telerik.Windows;  
    using Telerik.Windows.Controls;  
     
    namespace SilverlightApplication26  
    {  
        public partial class Page : UserControl  
        {  
            public Page()  
            {  
                InitializeComponent();  
                radContextMenu1.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));  
            }  
     
            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 == "Item 1")  
                {  
                    item.Icon = new Image() { Source = new BitmapImage(new Uri("rasterCard3D.png", UriKind.Relative)) };  
                }  
                else 
                {  
                    item.Icon = new Image() { Source = new BitmapImage(new Uri("rasterCard3DMonoChrome.png", UriKind.Relative)) };  
                }  
            }  
        }  

    Let us know if you need more help.

    Best wishes,

    Hristo
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. runes
    runes avatar
    15 posts
    Member since:
    Mar 2009

    Posted 25 Mar 2009 Link to this post

    Thank,
    but  how do i do  it in loded the contax menu??????????
    i want that the icons added in loded the contax menu.
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 25 Mar 2009 Link to this post

    Hello runes,

    I'm not sure that I understand your question. What do you mean with in Loaded event?

    You can add the icons in XAML or you can add them in the Page constructor. Just give Name to the contextMenu so that you have access in the code behind and after the InitializeComponent method iterate the Items of the contextMenu and set their Icon property (like in my previous post).

    Let me know if you need more help.

    Regards,

    Hristo
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  6. Mohit Vijay
    Mohit Vijay avatar
    11 posts
    Member since:
    Aug 2009

    Posted 27 Aug 2009 Link to this post

    Hello Telerik People,

    Can you please convert below code for vb.net?

    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 == "Item 1")  
                {  
                    item.Icon = new Image() { Source = new BitmapImage(new Uri("rasterCard3D.png", UriKind.Relative)) };  
                }  
                else 
                {  
                    item.Icon = new Image() { Source = new BitmapImage(new Uri("rasterCard3DMonoChrome.png", UriKind.Relative)) };  
                }  
            }  

  7. Nikolay
    Admin
    Nikolay avatar
    3997 posts

    Posted 31 Aug 2009 Link to this post

    Hi Mohit Vijay,

    I used converter.telerik.com and below is the result I got:


    Private Sub OnMenuItemClick(sender As Object, e As RoutedEventArgs) 
     Dim args As RadRoutedEventArgs = TryCast(e, RadRoutedEventArgs) 
     Dim item As RadMenuItem = TryCast(args.OriginalSource, RadMenuItem) 
     Dim header As String = Convert.ToString(item.Header) 
     If header = "Item 1" Then 
      item.Icon = New Image() 
     Else 
      item.Icon = New Image() 
     End If 
    End Sub 

    Hope this helps.

    All the best,
    Nick
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
DevCraft banner