c# - PropertyChanged is ignored when Binding to IEnumerable where reference stays the same -
i created minimal example illustrate binding issue. ienumerable<string> newreference
gets updated expected. ienumerable<string> samereference
not updated, presumably because reference same. raise("samereference");
not enough make wpf update reference.
is there can done make wpf framework revaluate samereference
though has same reference?
xaml:
<window x:class="stackoverflow.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"> <grid background="azure"> <grid.rowdefinitions> <rowdefinition height="5*"/> <rowdefinition height="1*"/> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition/> <columndefinition/> </grid.columndefinitions> <button content="update" margin="5" grid.row="1" grid.columnspan="2" click="button_click" /> <listbox itemssource="{binding samereference}" margin="5" /> <listbox itemssource="{binding newreference}" margin="5" grid.column="1" /> </grid> </window>
xaml.cs:
using system.collections.generic; using system.collections.objectmodel; using system.componentmodel; using system.windows; using system.windows.controls; namespace stackoverflow { public partial class mainwindow : window , inotifypropertychanged { public list<string> data = new list<string> { }; public ienumerable<string> samereference { { return data; } } //this returns reference unchanged object public ienumerable<string> newreference { { return new list<string>(data); } } //this returns reference new object //observablecollection<string> conventional known not point of question public event propertychangedeventhandler propertychanged; private void raise(string propertyname) { if(null != propertychanged) { propertychanged(this, new propertychangedeventargs(propertyname)); } } public mainwindow() { this.datacontext = this; initializecomponent(); } private void button_click(object sender, routedeventargs e) { data.add("this data."); raise("samereference"); //successful notify, ignored values raise("newreference"); //successful notify, processed values } } }
that intentional behavior. if want collections update implement inotifycollectionchanged
(or use class does, observablecollection<t>
).
Comments
Post a Comment