c# - WPF Binding Works Only First Time for Base View Model Properties -
we have item details window uses instance of viewmodel datacontext. have simple string property on viewmodel called statusmessage used give feedback user. we've implement inotifypropertychanged on view model , statusmessage property configured raise event. viewmodel has 3 other members classes (or collections) , on of these work bindings work should.
the problem statusmessage doesn't updated in gui when gets changed programmatically. we've debugged , found property changing , on window code behind subscribed property changed event , can see event firing.
any control bound statusmessage display whatever set on constructor, never updates after that.
i used 2 different controls, 1 textbox twoway binding , other label, both bound statusmessage. when change value using textbox, label changes. changes made inside viewmodel never propagated gui.
there no binding errors. we've checked hasbindingerrors property on propertychanged event handler , shows false , shows our control still bound statusmessage.
this problem affects base/root viewmodel class. members classes , have own members , implement inotifypropertychanged work without problems. have full 2 way binding , work.
i've tried cleaning , rebuilding project , doesn't make difference.
what can possibly cause binding fail without producing error messages?
this actual code although i've had trim out lot in order make fit nicely here:
the xaml:
<window x:class="pzespecsapp.specdetails" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:local="clr-namespace:specsapp" title="spec details" height="600" width="1000"> <grid> <textblock text="{binding spec.makeandmodelno}" fontsize="24" fontweight="bold" margin="16,10,0,0" /> <textbox name="txtstatusmessage" text="{binding statusmessage, mode=twoway, updatesourcetrigger=propertychanged}" fontsize="24" fontweight="bold" foreground="black" /> <label grid.column="0" content="{binding path=statusmessage}" foreground="white" /> </grid> </window> the window code:
public partial class specdetails : window { private specdetailsviewmodel model; public specdetails(int id) { initializecomponent(); model = new specdetailsviewmodel(id); model.propertychanged += viewmodel_propertychanged; datacontext = model; } private void viewmodel_propertychanged(object sender, system.componentmodel.propertychangedeventargs e) { //this shows property changes expected debug.print("propertychanged=" + e.propertyname); } } the viewmodel code:
public class specdetailsviewmodel { public event propertychangedeventhandler propertychanged; private spec _spec; public spec spec { { return _spec; } set { _spec = value; propertychanged.notify(() => this.spec); } } private string _statusmessage; public string statusmessage { { return _statusmessage; } set { _statusmessage = value; propertychanged.notify(() => this.statusmessage); } } public specdetailsviewmodel(int id) { spec = appdata.specslist.firstordefault(s => s.id == id); statusmessage = "monkey see, monkey do!"; //this stays on gui } public void savespec() { //this doesn't have affect on gui this.statusmessage = "changes saved"; } }
ok, found problem. guess implicitly implementing inotifypropertychanged , apparently that's not enough. fix in viewmodel class:
public class specdetailsviewmodel : inotifypropertychanged
Comments
Post a Comment