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

    Question

  • 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",
             UriKind.RelativeOrAbsolute))
    }

    (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">
                <Setter.Value>
                    <Rectangle Width="16" Height="16">
                        <Rectangle.Fill>
                            <ImageBrush ImageSource="/Sys2.SL.Core;component/Assets/png/16x16/delete.png"/>
                        </Rectangle.Fill>
                    </Rectangle>
                </Setter.Value>
            </Setter>
        </Style>
    (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

    <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:

    http://sl4popupmenu.codeplex.com 

    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

                    ctxMenu.Items.Add(MenuItem)

    Tuesday, May 15, 2012 5:41 AM