c# - Two-way binding in AvalonEdit doesn't work -
i have used avalonedit in project based on wpf , mvvm. after reading this post created next class:
public class mvvmtexteditor : texteditor, inotifypropertychanged { public static dependencyproperty documenttextproperty = dependencyproperty.register("documenttext", typeof(string), typeof(mvvmtexteditor), new propertymetadata((obj, args) => { mvvmtexteditor target = (mvvmtexteditor)obj; target.documenttext = (string)args.newvalue; }) ); public string documenttext { { homecoming base.text; } set { base.text = value; } } protected override void ontextchanged(eventargs e) { raisepropertychanged("documenttext"); base.ontextchanged(e); } public event propertychangedeventhandler propertychanged; public void raisepropertychanged(string info) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(info)); } } }
and used next xaml utilize control:
<avalonedit:mvvmtexteditor x:name="xmlmessage"> <avalonedit:mvvmtexteditor.documenttext> <binding path ="messagexml" mode="twoway" updatesourcetrigger="propertychanged"> <binding.validationrules> <local:xmlmessagevalidationrule /> </binding.validationrules> </binding> </avalonedit:mvvmtexteditor.documenttext> </avalonedit:mvvmtexteditor>
but binding works oneway
, doesn't update string property nor run validation rule.
how can prepare binding work expected twoway
?
wpf bindings not utilize documenttext
property; instead access underlying value of dependency property directly.
your ontextchanged
method doesn't alter value of underlying dependency property. need re-create value base.text
dependency property on every change:
protected override void ontextchanged(eventargs e) { setcurrentvalue(documenttextproperty, base.text); base.ontextchanged(e); }
this issue easier see if followed right pattern implementing dependencyproperty
: documenttext
property should utilize getvalue
/setvalue
methods, , not access different backing store.
c# wpf data-binding dependency-properties avalonedit
No comments:
Post a Comment