WPF setting a MenuItem.Icon in code


Question

I have an images folder with a png in it. I would like to set a MenuItem's icon to that png. How do I write this in procedural code?

1
33
1/11/2019 10:09:09 AM

Accepted Answer

menutItem.Icon = new System.Windows.Controls.Image 
       { 
           Source = new BitmapImage(new Uri("images/sample.png", UriKind.Relative)) 
       };
55
12/2/2011 7:15:10 AM

<MenuItem>
  <MenuItem.Icon>
    <Image>
      <Image.Source>
        <BitmapImage UriSource="/your_assembly;component/your_path_here/Image.png" />
      </Image.Source>
    </Image>
  </MenuItem.Icon>
</MenuItem>

Just make sure your image in also included in the project file and marked as resource, and you are good to go :)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon