I have a WPF contextmenu and I want to add an Item with Icon to this in my C# code. How Can I Do This???
Asked
Active
Viewed 3,210 times
2 Answers
2
There were already questions like this. For example here. So that you wouldn't have to drill down here:
menuItem.Icon = new BitmapImage(new Uri("images/sample.png", UriKind.Relative));

Community
- 1
- 1

Nikola Radosavljević
- 6,871
- 32
- 44
-
thanks.But What is The Namespace Of BitmapImage???Can I Use System.Drawing.Bitmap??? – ahmadali shafiee Oct 28 '11 at 12:28
-
System.Windows.Media.Imaging.BitmapImage in in PresentationCore.dll – Nikola Radosavljević Oct 28 '11 at 12:35
1
Should work for you.
<ContextMenu x:Key="MyContextMenu" >
<MenuItem Header="Item1" Click="Ietm1_Click"
Icon="{StaticResource ItemIco}" />
</ContextMenu>
and someone in resources reachable from this cose
<Image x:Key="ItemIco" Source="Images/ItemIco.png"/>
Where Images
, natuarally, is a folder of realtive path, in this case.

Tigran
- 61,654
- 8
- 86
- 123
-
No. I Have A DLL(Class Library) And I Don't Have Any Access To XAML Code.I Want to Do it In My C# Code – ahmadali shafiee Oct 28 '11 at 12:12