wpfimagemenuitemgeometrydrawing

Using a DrawingImage as Icon for Multiple MenuItems


I have some icon resources as DrawingImages that is made up of many GeometryDrawings. I have File MenuItems and ToolBar buttons that use these images via resource bindings to MenuItem.Icon. Unfortunately, only one of the MenuItems show the icon.

I am sure you can't assign a single DrawingImage resource to many MenuItem.Icon (or anything else for that matter), but I don't know of an alternative. I would prefer not to duplicate the DrawingImage resource, but if I have too I guess I will.


Solution

  • You assign an Image control to the Icon Property and set the DrawingImage into the Image.Source property.

    In XAML:

    <MenuItem>
        <MenuItem.Icon>
            <Image Source="{StaticResource myDrawingImage}"/>
        </MenuItem.Icon>
        <!-- everyhting else -->
    </MenuItem>
    

    In C#:

    menuItem.Icon = new Image() {Source = (ImageSource)Resources["myDrawingImage"]};