c# - Updating ListBox with changes in item property in BindlingList -



c# - Updating ListBox with changes in item property in BindlingList -

when alter property in item in bindinglist, changes not propagate listbox although underlying object changed.

public class myclass : inotifypropertychanged { public override string tostring() { homecoming this.name; } #region name private string name; public string name { { homecoming this.name; } set { if (value == this.name) return; this.name = value; this.onpropertychanged("name"); } } #endregion #region inotifypropertychanged event ///<summary> ///occurs when property value changes. ///</summary> public event propertychangedeventhandler propertychanged; /// <summary> /// raises <see cref="propertychanged"/> event /// given property. /// </summary> /// <param name="propertyname">the name of changed property.</param> protected void onpropertychanged(string propertyname) { //validate property name in debug builds verifyproperty(propertyname); if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } /// <summary> /// verifies whether current class provides property given /// name. method invoked in debug builds, , results in /// runtime exception if <see cref="onpropertychanged"/> method /// beingness invoked invalid property name. may happen if /// property's name changed not parameter of property's /// invocation of <see cref="onpropertychanged"/>. /// </summary> /// <param name="propertyname">the name of changed property.</param> [conditional("debug")] private void verifyproperty(string propertyname) { type type = this.gettype(); //look *public* property specified name propertyinfo pi = type.getproperty(propertyname); if (pi == null) { //there no matching property - notify developer string msg = "onpropertychanged invoked invalid property name {0}: "; msg += "{0} not public property of {1}."; msg = string.format(msg, propertyname, type.fullname); debug.fail(msg); } } #endregion }

and xaml

<window x:class="testbl.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <stackpanel> <listbox x:name="mylistbox"> </listbox> <button x:name="changebutton" click="changebutton_click">change item</button> </stackpanel> </window>

because binding listbox collection, default info context each listboxitem instance. collection illustration observablecollection<myclass> , info context each listboxitem myclass instance.

since haven't provided info template, listboxitem bound "{binding}" results in myclass.tostring() method beingness called. since tostring() isn't property, doesn't back upwards property alter notification. using binding in way (binding tostring()), works immutable objects.

the solution provide explicit binding listboxitem this:

<listbox.itemtemplate> <datatemplate> <textblock text="{binding name}"/> </datatemplate> </listbox.itemtemplate>

or create myclass object immutable , replace them instead of mutating them.

c# wpf binding

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -