How to set the icon for image contextMenu items

10,334

Actually it should work if you write :

<MenuItem.Icon>
  <Image Source="Images/reset.png" ></Image>
</MenuItem.Icon>

Just take care of right clicking to the properties of the images in your project, set it as Content, and Copy if newer.

Have a look at : WPF image resources

Regards

Share:
10,334
Arul karnan
Author by

Arul karnan

Updated on June 04, 2022

Comments

  • Arul karnan
    Arul karnan almost 2 years

    When I click image it displayed the menu but icon's are not displayed. I tried in two ways:

    1. One is I resized the icon that's not working
    2. Second one is I set the path using icon property that's not working.

    What's the way to set the icon for context menu items??

    Xaml:

    <Image Height="20" Width="20" Source="/CitiCall.WinClient;component/Images/user_icon.png" MouseDown="image1_MouseDown"  Margin="0,0,4,6" HorizontalAlignment="Right"  Name="image1" Stretch="Fill" VerticalAlignment="Top">               
        <Image.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Reset password" Icon="/CitiCall.WinClient;component/Images/reset.png"/>
                    <!--<MenuItem.Icon>
                        <Image Source="/CitiCall.WinClient;component/Images/reset.png" ></Image>
                    </MenuItem.Icon>
                </MenuItem>-->
                <MenuItem Header="Edit Profile"/>
                <MenuItem Header="Settings"/>
                <MenuItem Header="About us"/>
            </ContextMenu>
        </Image.ContextMenu>
    </Image>
    

    Xamal.cs:

    private void image1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
        {
            Image image = sender as Image;
            ContextMenu contextMenu = image.ContextMenu;                
            contextMenu.PlacementTarget = image;
            contextMenu.IsOpen = true;
        }
    }
    
  • Arul karnan
    Arul karnan over 8 years
    Yes the posted link is very useful. .