c# - WPF Textbox Binding Is Not Updating When Setting Value -


i have small program test out textbox databinding , works when change value in textbox itself, when try change value code-behind, variable updated textbox not updated. have looked around have not been able find solution, have seen textbox updating variable, opposite of need. here code:

xaml:

<grid>     <textbox text="{binding firstname, mode=twoway, updatesourcetrigger=propertychanged}" horizontalalignment="left" height="23" margin="127,37,0,0" textwrapping="wrap" verticalalignment="top" width="120"/>     <textbox text="{binding lastname, mode=twoway, updatesourcetrigger=propertychanged}" horizontalalignment="left" height="23" margin="127,88,0,0" textwrapping="wrap" verticalalignment="top" width="120"/>     <textblock horizontalalignment="left" margin="28,40,0,0" textwrapping="wrap" text="first name" verticalalignment="top"/>     <textblock horizontalalignment="left" margin="28,91,0,0" textwrapping="wrap" text="last name" verticalalignment="top"/>     <button x:name="name" content="name!" horizontalalignment="left" margin="145,212,0,0" verticalalignment="top" width="75" click="name_click"/>  </grid> 

and code:

namespace wpfapplication1 { public class uicontrols : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;     private string _firstname;     private string _lastname;      protected void notify(string propertyname)     {         if(this.propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     }      public string firstname     {         { return _firstname; }         set         {             if (value != _firstname)             {                 _firstname = value;                 notify("firstname");             }          }     }     public string lastname     {         { return _lastname; }         set         {             if (value != _lastname)             {                 _lastname = value;                 notify("lastname");             }         }     } }  public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();         uicontrols viewmodel = new uicontrols();         this.datacontext = viewmodel;     }      private void name_click(object sender, routedeventargs e)     {         uicontrols ui = new uicontrols();         ui.firstname = "mike";         ui.lastname = "smith";     } } 

}

well creating new uicontrols on every button click. new uicontrols not binded text box. try create private filed binded mainwindow, this:

public partial class mainwindow : window {      private uicontrols viewmodel;     public mainwindow()     {         initializecomponent();         viewmodel = new uicontrols();         this.datacontext = viewmodel;     }      private void name_click(object sender, routedeventargs e)     {         viewmodel.firstname = "mike";         viewmodel.lastname = "smith";     } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -