c# - How to bind to an unbindable property without violating MVVM? -
i using sharpvector's svgviewbox show static resource images this:
<svgc:svgviewbox source="/resources/label.svg"/>
which works fine. however, wish control image shown through binding view model.
the problem i'm experiencing source
property of svgviewbox not bindable.
how can around limitation without violating mvvm (e.g., passing control view model , modifying there)?
what looking called attached properties. msdn offers topic on title "custom attached properties"
in case may simple this
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 svgviewbox; if (svgcontrol != null) { svgcontrol.source = new uri((string)e.newvalue); } } public static readonly dependencyproperty sourceproperty = dependencyproperty.registerattached("source", typeof (string), typeof (svgviewboxattachedproperties), // default value: null new propertymetadata(null, onsourcechanged)); } }
xaml use it
<svgviewbox margin="0 200" local:svgviewboxattachedproperties.source="{binding path=imagepath}" />
note local
namespace prefix , should point assembly/namespace class located at, i.e. xmlns:local="clr-namespace:myproject.extensions;assembly=myproject"
.
then use attached property (local:source
) , never source
property.
Comments
Post a Comment