Featured post
c# - WPF Binding Issue - UI Updates, Object Does Not -
i'm having yet wpf binding issue. when think i've got stuff figured out, run more problems... :s
anyway... i've created custom user control selecting files. it's simple textbox followed button contained within grid. property of control working called filepath , textbox on control bound property. when button clicked, savefiledialog opened , user selects file. ui correctly updates after user selects file.
the problem seem having when bind object control (in instance have object documentfilepath property) object doesn't update when new file selected.
here's relevant code within user control:
public static readonly dependencyproperty filepathproperty = dependencyproperty.register("filepath", typeof(string), typeof(filesave), new uipropertymetadata(string.empty, onfilepathchanged)); public string filepath { { return this.getvalue(filepathproperty) string; } set { this.setvalue(filepathproperty, value); this.onpropertychanged("filepath"); } } private void onpropertychanged(string propname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propname)); } } private static void onfilepathchanged(object sender, dependencypropertychangedeventargs e) { ((filesave)sender).onpropertychanged("filepath"); }
and user control added window programatically using reflection on object:
private void addfilesave(propertyinfo pi) { filesave fs = new filesave(); binding b = new binding(pi.name); fs.setbinding(filesave.filepathproperty, b); this.addtogrid(fs); //adds control window's grid in correct row , column; nothing fancy here }
it may worth noting if load window existing object, user control displays still won't register changes within object bound.
please let me know if guys need more info.
thanks in advance,
sonny
edit: i've found way around problem, isn't solution. watching debugger found when set filepath property within control, object being unbound. if can shed light on that, appreciative. in mean time, i've changed code opens savefiledialog this:
private void button_click(object sender, routedeventargs e) { microsoft.win32.openfiledialog ofd = new microsoft.win32.openfiledialog(); ofd.multiselect = false; ofd.title = "select document import..."; ofd.validatenames = true; ofd.showdialog(); if (this.getbindingexpression(filepathproperty) == null) { this.filepath = ofd.filename; } else //set value on bound object (this new portion added) { bindingexpression = this.getbindingexpression(filepathproperty); string propname = be.parentbinding.path.path; object entity = be.dataitem; system.reflection.propertyinfo pi = entity.gettype().getproperty(propname); pi.setvalue(entity, ofd.filename, null); } if (!string.isnullorwhitespace(this.filepath)) { _filecontents = new memorystream(); using (streamreader sr = new streamreader(this.filepath)) { _filecontents = new memorystream(system.text.asciiencoding.ascii.getbytes(sr.readtoend())); } } else { _filecontents = null; } }
you're not specifying anywhere in code filepath property should twoway updates of dp value won't pushed bound source object's property. can use either:
binding b = new binding(pi.name){ mode = bindingmode.twoway };
or can set dependency property use default of twoway:
public static readonly dependencyproperty filepathproperty = dependencyproperty.register( "filepath", typeof(string), typeof(filesave), new frameworkpropertymetadata(string.empty, frameworkpropertymetadataoptions.bindstwowaybydefault, onfilepathchanged));
you should follow robert's suggestion of removing manual propertychange event, , don't ever add code other getvalue , setvalue in dp wrapper property. xaml calls getvalue , setvalue directly skip on else add there - can lead nasty bugs.
- Get link
- X
- Other Apps
Comments
Post a Comment