如何在不违反MVVM的情况下绑定到不可绑定的属性?

Luk*_*asz 6 c# data-binding wpf svg mvvm

我使用SharpVector的SvgViewBox来显示这样的静态资源图像:

<svgc:SvgViewbox Source="/Resources/label.svg"/>
Run Code Online (Sandbox Code Playgroud)

哪个工作正常.但是,我希望通过绑定到视图模型来控制显示的图像.

我遇到的问题是SourceSvgViewbox 的属性不可绑定.

如何在不违反MVVM的情况下解决此限制(例如,将控件传递给视图模型并在其中进行修改)?

Tse*_*eng 8

您正在寻找的是所谓的附加属性.MSDN提供了一个标题为" 自定义附加属性 "的主题

在你的情况下,它可能看起来像这样简单

namespace MyProject.Extensions 
{
    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)
            {
                var path = (string)e.NewValue;
                svgControl.Source = string.IsNullOrWhiteSpace(path) ? default(Uri) : new Uri(path);
            }                
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.RegisterAttached("Source",
                typeof (string), typeof (SvgViewboxAttachedProperties),
                                    // default value: null
                new PropertyMetadata(null, OnSourceChanged));
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML使用它

<SvgViewbox Margin="0 200" 
    local:SvgViewboxAttachedProperties.Source="{Binding Path=ImagePath}" />
Run Code Online (Sandbox Code Playgroud)

请注意,这local是命名空间前缀,它应指向该类所在的程序集/命名空间,即xmlns:local="clr-namespace:MyProject.Extensions;assembly=MyProject".

然后只使用您附加的属性(local:Source)而不是Source属性.

新附加属性local:Source的类型为System.Uri.要更新映像,首先再分配null,然后再次分配文件名/文件路径.