Using vector graphics as an icon in WPF

I have the following base class for MenuItems

in my MVVM application

public class StandardMenuItem : MenuItemBase, IExecutableItem
{
    ...
    public Image Icon { get; private set; }
    ...
}

      

where my initial idea was to use Image

to return the icons displayed on mine MenuItems

. Now I've come to the point where I started using these MenuItems

in the front end of my application and found an excellent vector graphics library that I want to use instead.

<ResourceDictionary x:Class="resources_icons_xaml"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Canvas x:Key="appbar_acorn" Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0">
        <Path Width="22.3248" Height="25.8518" Canvas.Left="13.6757" Canvas.Top="11.4012" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 16.6309,18.6563C 17.1309,8.15625 29.8809,14.1563 29.8809,14.1563C 30.8809,11.1563 34.1308,11.4063 34.1308,11.4063C 33.5,12 34.6309,13.1563 34.6309,13.1563C 32.1309,13.1562 31.1309,14.9062 31.1309,14.9062C 41.1309,23.9062 32.6309,27.9063 32.6309,27.9062C 24.6309,24.9063 21.1309,22.1562 16.6309,18.6563 Z M 16.6309,19.9063C 21.6309,24.1563 25.1309,26.1562 31.6309,28.6562C 31.6309,28.6562 26.3809,39.1562 18.3809,36.1563C 18.3809,36.1563 18,38 16.3809,36.9063C 15,36 16.3809,34.9063 16.3809,34.9063C 16.3809,34.9063 10.1309,30.9062 16.6309,19.9063 Z "/>
    </Canvas>
    ...
<ResourceDictionary/>

      

My problem is that using this vector art through code doesn't seem straight forward. I know how to include graphics like this in XAML

<!-- Include Resource Dictionary -->
<MenuItem Header="Show Difference Details" 
          ToolTip="Launch the grouped data file and analysis window." 
          IsEnabled="{Binding GroupedDataIsDifferent}"
          Caliburn:Message.Attach="[Event Click] = [Action ShowDifferenceDetailsAsync()]">
    <MenuItem.Icon>
        <Rectangle Width="16" Height="16">
            <Rectangle.Fill>
                <VisualBrush Stretch="Uniform" Visual="{StaticResource appbar_column_two}" />
            </Rectangle.Fill>
        </Rectangle>
    </MenuItem.Icon>
</MenuItem>

      

but that's not my problem. My questions:

  • How do I use vector graphics from the resource dictionary in my signs / images for my StandardMenuItem

    s?
  • If the answer is 1. "you can't", how can I convert from vector graphics to Icon

    in code?

Thank you for your time.


Edit. I want to get graphics using code. So, for my menu item, I have a method

public StandardMenuItem WithIcon(Assembly source, string path)
{
    var manager = IoC.Get<IResourceManager>();
    var iconSource = manager.GetBitmap(path, source.GetAssemblyName());
    if (source != null)
    {
        IconSource = path;
    }
    return this;
}

      

My problem is now getting the correct path to the vector image I want. Let's say in my solution I have my vector image in "Graphics / Icons.xaml" and the resource is called "appbar_acorn", how can I reference this?

+3


source to share


1 answer


here you go

start by changing the icon property to a string

eg,

public string Icon { get; private set; }

      

assign the icon value as the icon key you want to use

Icon = "appbar_acorn";

      

define the converter in resources

<l:StringToResourceConverter x:Key="StringToResourceConverter" />

      



l: refers to the namespace of the transformer, for example xmlns:l="clr-namespace:CSharpWPF"

using

<MenuItem Icon="{Binding Icon,Converter={StaticResource StringToResourceConverter}}"
          Header="Menu"/>

      

result

result

here is the converter class

namespace CSharpWPF
{
    class StringToResourceConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Application.Current.FindResource(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

      

you may need to adjust the size and placement of the icons on the canvas, in the above example I removed Canvas.Left="13.6757" & Canvas.Top="11.4012"

, but still it's a bit big for a menu icon

+4


source







All Articles