Reputation: 11111
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?
Upvotes: 41
Views: 62219
Reputation: 27065
<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 :)
Upvotes: 23
Reputation: 441
This is what worked for me
<MenuItem Header="delete ctrl-d" Click="cmiDelete_Click">
<MenuItem.Icon>
<Image>
<Image.Source>
<ImageSource>Resources/Images/delete.png</ImageSource>
</Image.Source>
</Image>
</MenuItem.Icon>
</MenuItem>
Upvotes: 1
Reputation: 39
This is a bit shorter :D
<MenuItem Header="Example">
<MenuItem.Icon>
<Image Source="pack://siteoforigin:,,,/Resources/Example.png"/>
</MenuItem.Icon>
</MenuItem>
Upvotes: 3
Reputation: 415
For those of you using vb.net, to do this you need to use this:
menuItem.Icon = New Image() With {.Source = New BitmapImage(New Uri("pack://application:,,,/your_assembly;component/yourpath/Image.png"))}
Upvotes: 0
Reputation: 2233
menutItem.Icon = new System.Windows.Controls.Image
{
Source = new BitmapImage(new Uri("images/sample.png", UriKind.Relative))
};
Upvotes: 70
Reputation: 4803
Arcturus's answer is good because it means you have the image file in your project rather than an independent folder.
So, in code that becomes...
menutItem.Icon = new Image
{
Source = new BitmapImage(new Uri("pack://application:,,,/your_assembly;component/yourpath/Image.png"))
}
Upvotes: 18
Reputation: 1
You can also use your Visual Studio to insert a icon. This is the easiest way
Problem solved.
Upvotes: -5
Reputation: 22460
This is how I used it (this way it dont need to be built into the assembly):
MenuItem item = new MenuItem();
string imagePath = "D:\\Images\\Icon.png");
Image icon = new Image();
icon.Source= new BitmapImage(new Uri(imagePath, UriKind.Absolute));
item.Icon = icon;
Upvotes: 2