SL4 ContextMenu: is it possible set MenuItem.Icon programmatically?


  • i try do this:

    Icon = new Rectangle() {
       Width = 16, Height = 16, Fill = new ImageBrush() {
          ImageSource = new BitmapImage(new Uri(@"/Sys2.SL.Core;component/Assets/png/16x16/delete.png",

    (icon is not visible)

    ,try this:

    Style = Application.Current.Resources["MenuItemDeleteStyle"] as Style

    where MenuItemDeleteStyle:
        <Style x:Key="MenuItemDeleteStyle" TargetType="inputToolkit:MenuItem">
            <Setter Property="Icon">
                    <Rectangle Width="16" Height="16">
                            <ImageBrush ImageSource="/Sys2.SL.Core;component/Assets/png/16x16/delete.png"/>
    (Unhandled Error in Silverlight Application)

    i cannot find what is wrong ((
    there are two ContextMenu for dataform (one for readonly and one for edit modes), and ContextMenuService.SetContextMenu(dataForm, <ctxMenu4actualDfMode>) is using to switch between them - thats why cann't use xaml...

    p.s.excuse my English :)

    Sunday, April 25, 2010 6:30 PM

All replies

  • workaround is to place ImageBrush into UserControl.Resources

        <ImageBrush x:Key="DeleteImageBrush" ImageSource="/Sys2.SL.Core;component/Assets/png/16x16/delete.png"/>

    and then, in code

    Icon = new Rectangle() { Width = 16, Height = 16, Fill = this.Resources["DeleteImageBrush"] as ImageBrush } (icon is visible)

    but, if place it into app resources (another assembly), its not work

    Icon = new Rectangle() { Width = 16, Height = 16, Fill = Application.Current.Resources["DeleteImageBrush"] as ImageBrush } (icon is not visible)

    why? what am i missing?

    Monday, April 26, 2010 4:35 PM
  • I have this problem too!! please help!!

    Wednesday, July 21, 2010 6:38 AM
  • If you still haven't found a workaround then why not try the context menu from this project instead: 

    Tuesday, August 24, 2010 7:46 PM
  • In fact it is quite easy:

    1. Create a bitmapimage

                    Dim bmp As BitmapImage
                    bmp = New BitmapImage(New Uri("/xxx;component/Images/icons/ContextMenu/Copy16.png", UriKind.Relative))

    2. Add a image control (XAML) and add the bitmapimage as its source

                   Dim img As Image = New Image()
                    img.Source = bmp

    3. Add the image control as the icon content.
                    MenuItem.Icon = img


    Tuesday, May 15, 2012 5:41 AM