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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matt CartlidgeSenior Infrastructure EngineerAuthor Commented:
Thanks :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WPF

From novice to tech pro — start learning today.