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

Icons in contaxMenu

5 Answers 104 Views
Menu
This is a migrated thread and some comments may be shown as answers.
runes
Top achievements
Rank 1
runes asked on 24 Mar 2009, 11:09 AM
Hi,
I want to add icons in contaxMenu dinamicly.(in run time).
How I do it?
Thanks.

5 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 24 Mar 2009, 05:51 PM

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.
0
runes
Top achievements
Rank 1
answered on 25 Mar 2009, 07:37 AM
Thank,
but  how do i do  it in loded the contax menu??????????
i want that the icons added in loded the contax menu.
0
Hristo
Telerik team
answered on 25 Mar 2009, 07:56 AM

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.
0
Mohit Vijay
Top achievements
Rank 1
answered on 27 Aug 2009, 09:56 PM
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)) };  
            }  
        }  

0
Nikolay
Telerik team
answered on 31 Aug 2009, 03:11 PM
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.
Tags
Menu
Asked by
runes
Top achievements
Rank 1
Answers by
Hristo
Telerik team
runes
Top achievements
Rank 1
Mohit Vijay
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or