K
K
Kirill Frolov2016-02-27 17:45:32
WPF
Kirill Frolov, 2016-02-27 17:45:32

How to work with SVG in WPF?

In the ViewModel I get svg as a string. How to bind it in View as an icon? Do I need a converter or can I somehow do without it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Frolov, 2016-02-28
@f_kirill

Used SharpVectors .
In the ViewModel I save to the svg file.
In xaml markup I use the SvgViewbox component.
To bind the path to Source added AttachedProperty

public class SvgViewboxAttachedProperties : DependencyObject
    {
        public static string GetSource(DependencyObject obj)
        {
            return (string)obj.GetValue(SourceProperty);
        }

        public static void SetSource(DependencyObject obj, string value)
        {
            obj.SetValue(SourceProperty, value);
        }

        private static void OnSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var svgControl = obj as SvgViewbox;
            if (svgControl != null)
            {
                svgControl.Source = new System.Uri((string) e.NewValue);
            }
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.RegisterAttached("Source",
                typeof(string), typeof(SvgViewboxAttachedProperties),
                new PropertyMetadata(null, OnSourceChanged));
    }

As a result, the markup looks like this
<svgc:SvgViewbox  common:SvgViewboxAttachedProperties.Source="{Binding IconPath}" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question