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

How to set icon from codebehind

2 Answers 894 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
PoorLeno
Top achievements
Rank 1
PoorLeno asked on 24 Dec 2010, 03:51 PM

I have the following code creating a context menu

var menu = new RadContextMenu();
var copyToCliboardMenuItem = new RadMenuItem {Header = "Copy Figure to Clipboard", Icon = ??); 
menu.Items.Add(copyToCliboardMenuItem);
RadContextMenu.SetContextMenu(BoundChart, menu);

Given the following stream, 

var stream = Application.GetResourceStream(new Uri("pack://application:,,,/AppName;component/Images/myicon.png" ));

how can I set the Icon property?

2 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 28 Dec 2010, 09:52 AM
Hello PoorLeno,

The icon property is an object type. This means that you can put whatever object you want. Follow the rewrite code bellow:

var menu = new RadContextMenu();
            var copyToCliboardMenuItem = new RadMenuItem
            {
                Header = "Copy Figure to Clipboard",
  
                Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/AppName;component/myicon.png")) }
            };
            menu.Items.Add(copyToCliboardMenuItem);
            RadContextMenu.SetContextMenu(rectangle, menu);


Greetings,
Kaloyan
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Lawrence
Top achievements
Rank 2
Iron
answered on 26 Oct 2016, 01:02 PM

I know that I'm 6 years late to the party, but here's a solution that allows you to use your resource image.

var menuItem = new RadMenuItem
  {
        Header = "Edit",
        Icon = new Image {Source = Resources.pencil24.ToBitmapImage()}
  }
 
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
{
    using (var memory = new MemoryStream())
    {
        bitmap.Save(memory, ImageFormat.Png);
        memory.Position = 0;
 
        var bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = memory;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();
 
        return bitmapImage;
    }
}

 

Tags
ContextMenu
Asked by
PoorLeno
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Lawrence
Top achievements
Rank 2
Iron
Share this question
or