MoonKnight
MoonKnight

Reputation: 23831

Using Vector Graphics as 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 back the icons I display on my MenuItems. I have now come to the point where I am starting to use these MenuItems in the front end of my application and have found a superb vector graphics library 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, using these vector graphics via code does not seem straight forward. I know how to include such graphics 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 this is not my problem. My questions are:

  1. How can I use vector graphics from a resource dictionary in my Icons/Images for my StandardMenuItems?
  2. If the answer to 1. is "you can't", how can I convert from a vector graphic to an Icon in code?

Thanks for your time.


Edit. I want to be able to pick up the 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 issue now is getting the correct path to the vector image I want. Lets 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?

Upvotes: 4

Views: 8312

Answers (1)

pushpraj
pushpraj

Reputation: 13679

here you go

start by changing the Icon property to string

eg

public string Icon { get; private set; }

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

Icon = "appbar_acorn";

define the converter in resources

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

l: refers to the converter's namespace eg xmlns:l="clr-namespace:CSharpWPF"

the usage

<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 icon size and placement on canvas, in the sample above I removed the Canvas.Left="13.6757" & Canvas.Top="11.4012" but still it is little big for the menu icon

Upvotes: 5

Related Questions