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

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

 

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

我遇到的问题是SvgViewbox的Source属性不可绑定。

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

您正在寻找的是被称为附加属性。 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)); } } 

XAML使用它

  

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

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

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