Link to home
Start Free TrialLog in
Avatar of Matt Cartlidge
Matt CartlidgeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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?
Avatar of Snarf0001
Snarf0001
Flag of Canada image

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.
Avatar of Matt Cartlidge

ASKER

Is there a way to set styles globally?
ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks :)