c# - Binding label to a variable -


i starting wpf , trying setup binding between local variable , label. basicaly want update label when local variable changes. searching solution use textbox source not class variable , not sure works way. here code.

public partial class mainwindow : window {     int idcounter;      public mainwindow()     {         initializecomponent();           binding b = new binding();         b.source = idcounter;         b.mode = bindingmode.oneway;         b.updatesourcetrigger = updatesourcetrigger.propertychanged;          mylabel.setbinding(label.contentproperty,b);     }      private void button_click(object sender, routedeventargs e)     {         idcounter++;     } } 

button work, idcounter changes value, not update in label guess binding wrong. can tell me wrong? thanks

your code work if change class this...

  public partial class window1 : window, inotifypropertychanged     {         private int _idcounter;         public int idcounter         {             { return _idcounter; }             set             {                 if (value != _idcounter)                 {                     _idcounter = value;                     onpropertychanged("idcounter");                 }             }         }         public window1()         {             initializecomponent();             mylabel.setbinding(contentproperty, new binding("idcounter"));             datacontext = this;         }         private void button_click(object sender, routedeventargs e)         {             e.handled = true;             idcounter++;         }         #region inotifypropertychanged implementation         public event propertychangedeventhandler propertychanged;         protected virtual void onpropertychanged(string name)         {             var handler = system.threading.interlocked.compareexchange(ref propertychanged, null, null);             if (handler != null)             {                 handler(this, new propertychangedeventargs(name));             }         }         #endregion     } 

some of issues having are...

the window should implement inotifypropertychanged binding engine can place ear on it.

the idcounter needs public , have public getter on binding engine can 'get' it.

you should set datacontext whatever class has declared idcounter (the mainwindow in case). part of problem binding engine had no datacontext.

the bindingmode setting red-herring since label binds way default.

the updatesourcetrigger red-herring since content of label not have mechanism update source property. label's content not text box user can type code needs know about. when you're binding user cannot change, forget updatesourcetrigger, it's target property counts.

the handler should mark event. practice , did not affect binding.

the binding constructor needs path.

this code give expected result; i.e., label updates when button clicked. checked, compiled, , executed on vs2013, .net 4.5.

the other respondents said should use view model. agree 100%, , overall it's thing consider.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -