Answered by:
WPF: MenuItem Icon

Question
-
I would like to add an icon to my MenuItems. Is this the smartest way to do this:
<MenuItem Header="Settings" Name="menuEditSettings" Click="menuEditSettings_Click">
<MenuItem.Icon>
<Image Height="16" Width="16" Source="Icons/menuSettings.png" />
</MenuItem.Icon>
</MenuItem>
Why would something like this not work:
<MenuItem Header="Settings" Name="menuEditSettings" Icon="Icons/menuSettings.png" Click="menuEditSettings_Click"/>
I just get the text as icon instead of the current bitmap!Wednesday, December 3, 2008 8:10 AM
Answers
-
Hi,
I would like to give in a suggestion here actually. It would be better here to give the image in Application Resources,and get that from there using the key.Doing in this way ,even if u want to change the image at a later point of time u need to change only at one place(if u are using the same icon in a multiple place) and need not change everywhere u have used it
Thank You
FEAR NOT TO BE JUST- Marked as answer by DennisMadsen Wednesday, December 3, 2008 1:02 PM
Wednesday, December 3, 2008 9:05 AM -
Hi,
You have to give the key name here.
Icon="{DynamicResource KeyName}"
Add the icon files to your project(using Add Existing item--right click on project).Club all the icons into a folder in your project if u want.Create a resource dictionary for your icons.Specify all Images in the resource dictionary(by giving source of image as \Folder\ImageName.extn) and associate with a key.Dont forget to put this resource dictionary entry in applications resources.
Then use the key from wherever you want as i have shown above
Hope it helps
FEAR NOT TO BE JUST- Proposed as answer by Parvez Ansari Wednesday, December 3, 2008 12:00 PM
- Marked as answer by DennisMadsen Wednesday, December 3, 2008 1:02 PM
Wednesday, December 3, 2008 11:53 AM
All replies
-
Hi,
I would like to give in a suggestion here actually. It would be better here to give the image in Application Resources,and get that from there using the key.Doing in this way ,even if u want to change the image at a later point of time u need to change only at one place(if u are using the same icon in a multiple place) and need not change everywhere u have used it
Thank You
FEAR NOT TO BE JUST- Marked as answer by DennisMadsen Wednesday, December 3, 2008 1:02 PM
Wednesday, December 3, 2008 9:05 AM -
Thanks. I do this:
Click my project -> Add -> Resource Dictionary.
Here I select "Icon file" and I got a default Icon.
After this I change my menuItem to this:
<MenuItem Header="_Settings" Name="menuEditSettings" Icon="Icon1.ico" Click="menuEditSettings_Click">
But still I see the String "Icon1.ico" instead of the icon. Hope you can help me?
Is there a easy way to import my PNG or JPEG image to the icon-file?Wednesday, December 3, 2008 11:14 AM -
Hi,
You have to give the key name here.
Icon="{DynamicResource KeyName}"
Add the icon files to your project(using Add Existing item--right click on project).Club all the icons into a folder in your project if u want.Create a resource dictionary for your icons.Specify all Images in the resource dictionary(by giving source of image as \Folder\ImageName.extn) and associate with a key.Dont forget to put this resource dictionary entry in applications resources.
Then use the key from wherever you want as i have shown above
Hope it helps
FEAR NOT TO BE JUST- Proposed as answer by Parvez Ansari Wednesday, December 3, 2008 12:00 PM
- Marked as answer by DennisMadsen Wednesday, December 3, 2008 1:02 PM
Wednesday, December 3, 2008 11:53 AM -
Thanks. Now I'm almost there!
I've a "Dictionary.xaml" like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ImageSource x:Key="imageWindowMain">Images/windowMain.png</ImageSource>
<ImageSource x:Key="imageMenuSettings">Images/menuSettings.png</ImageSource>
</ResourceDictionary>
How can I add this to my "App.xaml"?
Thanks!
Wednesday, December 3, 2008 12:07 PM -
I found the answer.
My Icon on the Window works - but still problems with my MenuItem:
<MenuItem Header="_Settings" Name="menuEditSettings" Icon="{DynamicResource imageMenuEditSettings}" Click="menuEditSettings_Click"/>
This gives me a string instead of the icon in the menu. The String says something like:
pack://application:,,,Icons/menuEditSettings.png
Hope you can help me?
Wednesday, December 3, 2008 12:25 PM -
Hi,
in App.xaml<ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ResourceDictionaryName.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
also its not ImageSource use
<Image Source="\Folder\ImageName.extn" x:Key="KEY"/>
Hope it helps
FEAR NOT TO BE JUSTWednesday, December 3, 2008 12:26 PM -
Well, after chaning to Image-type:
<Image Source="/Images/windowMain.png" x:Key="imageWindowMain"/>
Instead of ImageSource-type I get this exception on start up:
Cannot convert the value in attribute 'Icon' to object of type 'System.Windows.Media.ImageSource'. 'System.Windows.Controls.Image' is not a valid value for property 'Icon'.
Why do you say, that I should use the Image-type instead of ImageSource-type?
Wednesday, December 3, 2008 12:43 PM -
Well, I discover that I should use ImageSource for menuItems and Image for my Window.
Finally I get this:
<ImageSource x:Key="imageWindowMain">Images/windowMain.png</ImageSource>
<Image Source="/Images/menuEditSettings.png" x:Key="imageMenuEditSettings"/>
Is this the way to trick it?
Wednesday, December 3, 2008 12:48 PM -
Hi,
Was it working with ImageSource??
ImageSource is a abstract class i think.
And that error wont come .Hope the Image type u used in resource dictionary is of type Windows.Controls.Image
Thank You
PS: Please mark the post as answered if it helps you
FEAR NOT TO BE JUSTWednesday, December 3, 2008 12:53 PM