How to set icon from codebehind

3 posts, 0 answers
  1. PoorLeno
    PoorLeno avatar
    4 posts
    Member since:
    Dec 2010

    Posted 24 Dec 2010 Link to this post

    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. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 28 Dec 2010 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Lawrence
    Lawrence avatar
    10 posts
    Member since:
    Jul 2013

    Posted 26 Oct Link to this post

    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;
        }
    }

     

Back to Top