c# - WPF - ValidationRule isn't being called -
this follow question asked question me:
wpf - validationrule not beingness called
there told should implement inotifydataerrorinfo , did still doesn't work.
here xaml:
<textblock verticalalignment="center"> <textblock.text> <binding path="path" mode="twoway" updatesourcetrigger="propertychanged"> <binding.validationrules> <viewmodel:strrule/> </binding.validationrules> </binding> </textblock.text> </textblock> in viewmodel:
private string _path; public string path { set { _path = value; onpropertychange("path"); } { homecoming _path; } } public event propertychangedeventhandler propertychanged; private void onpropertychange(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } private dictionary<string, list<string>> errors = new dictionary<string, list<string>>(); public event eventhandler<dataerrorschangedeventargs> errorschanged; private void seterrors(string propertyname, list<string> propertyerrors) { // clear errors exist property. errors.remove(propertyname); // add together list collection specified property. errors.add(propertyname, propertyerrors); // raise error-notification event. if (errorschanged != null) errorschanged(this, new dataerrorschangedeventargs(propertyname)); } private void clearerrors(string propertyname) { // remove error list property. errors.remove(propertyname); // raise error-notification event. if (errorschanged != null) errorschanged(this, new dataerrorschangedeventargs(propertyname)); } public system.collections.ienumerable geterrors(string propertyname) { if (string.isnullorempty(propertyname)) { // provide error collections. homecoming (errors.values); } else { // provice error collection requested property // (if has errors). if (errors.containskey(propertyname)) { homecoming (errors[propertyname]); } else { homecoming null; } } } public bool haserrors { { homecoming errors.count > 0; } } and validation rule:
public class strrule : validationrule { public override validationresult validate(object value, system.globalization.cultureinfo cultureinfo) { string filepath = string.empty; filepath = (string)value; if (string.isnullorempty(filepath)) { homecoming new validationresult(false, "must give path"); } if (!file.exists(filepath)) { homecoming new validationresult(false, "file not found"); } homecoming new validationresult(true, null); } } i got button opens filedialog , updates viewmodels' path property.
when textblock updated, binding works , set property beingness called, not validation rule itself. missing/wrong here?
as stated in comment validationrule gets called in case binding updated target(textblock text) source(viewmodel property).
so, instead of setting source value straight vm.filespath = filename;, set textblock text value , validation rule validate method called.
c# wpf xaml validationrule
No comments:
Post a Comment