WPF/XAML - Add a XAML geometric image to a menu item as an icon

I've had the question answered regarding how to display a XAML image in a button.

            <Button Width="32" Height="32" MouseLeftButtonDown="Button_MouseLeftButtonDown" MouseDown="Button_MouseDown">
                <Viewbox VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill">
                    <ContentPresenter ContentTemplate="{StaticResource icon-drag}"/>
                </Viewbox>
            </Button>

Open in new window


However, I need to do the same as an icon on a menu item. This doesn't cut it.

        <Menu Grid.Column="0">
            <MenuItem Header="_File">
                <MenuItem Header="_New" Icon="{StaticResource icon-drag}"/>
            </MenuItem>
        </Menu>

Open in new window


I want to leave behind icon/PNG images as much as possible and use XAML geometry so that my images can be scaled just as the other XAML controls can be.

Is it possible?
Matt CartlidgeSenior Infrastructure EngineerAsked:
Who is Participating?
 
Snarf0001Commented:
Absolutely.  You should have an "App.xaml" file included in the project already.
In there, there's an "Application.Resources" node, where you can put resource dictionaries the same way you do in any other page.
Anything in there will be available globally.

If you have more than one resource file, or want to combine individual files and inline styles, use the <MergedDictionaries> element.

<Application x:Class="WPFTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFTest"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Style1.xaml" />
                <ResourceDictionary Source="/Style2.xaml" />
                <ResourceDictionary>
                    <!--custom styles not in a file-->
                </ResourceDictionary>
                <ResourceDictionary>
                    <!--more custom styles not in a file-->
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Open in new window

0
 
Snarf0001Commented:
Yes, you just still need to use the content presenter as you did with the Button:

<MenuItem Header="Item 1">
    <MenuItem.Icon>
        <Viewbox>
            <ContentPresenter ContentTemplate="{StaticResource img-hand}"/>
        </Viewbox>
    </MenuItem.Icon>
</MenuItem>

Open in new window


And just food for thought, I'd highly recommend you start putting this in styles.  Especially if you're going to be setting things like the alignment on every item, you can encompass all of that, including the path declerations into a style.
0
 
Matt CartlidgeSenior Infrastructure EngineerAuthor Commented:
Is there a way to set styles globally?
0
 
Matt CartlidgeSenior Infrastructure EngineerAuthor Commented:
Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.