c# - Binding a string variable to a label -


i trying move winforms wpf, , stuck on binding.

i have label:

    <label name="labelstate" content="{binding state}" horizontalalignment="right"  margin="10,10,10,10" fontsize="12" /> 

in cs of same usercontrol (named forminput), have :

 public string state { get; set; }    public forminput()         {             state = "ok";              initializecomponent();                   } 

why doesn't work?

thank you.

when binding in wpf need use inotifypropertychanged

implement class follows,

class testobject : inotifypropertychanged     {         private string _state;          public string state         {                         {                 return _state;             }              set             {                 if (_state == value) return;                  _state = value;                 onpropertychanged("state");             }         }          #region inotifypropertychanged members          public event propertychangedeventhandler propertychanged;          protected virtual void onpropertychanged(string propertyname)         {             if (propertychanged != null)             {                 propertychanged(this, new propertychangedeventargs(propertyname));             }         }          #endregion     } 

and in forminput

public forminput() {   initializecomponent();   testobject t = new testobject();   labelstate.datacontext = t;   t.state = "ok"; } 

and xaml follows,

  <label name="labelstate" content="{binding state}" horizontalalignment="right"    > 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -