c# - Why PropertyChanged could be null when gets binded? -



c# - Why PropertyChanged could be null when gets binded? -

here problem encountering, utilize class names in demo describe problem:

i have show on datagrid, view of item. item wrapper class of real info source, dummy.

container manager of dummy's, phone call poll periodically(in demo, 1s) , internally queries remote server decide current value(in demo, flip boolean). , after that, container go through item's , notify them changes may made.

but problem is, if branch in notify called container won't in, since propertychanged null! if alter in datagrid, i.e. dg, double click, event gets subscribed. problem is, how can notify in container? there way create datagrid subscribe propertychanged time?

btw, if utilize debugger in, null too.

following demo code:

xaml:

<window x:class="wpfapplication6.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> <datagrid x:name="dg" margin="0,0,0,35"/> <button content="button" margin="0,0,10,10" height="20" verticalalignment="bottom" horizontalalignment="right" width="75" click="button_click"/> </grid> </window>

c# part:

using system; using system.collections.generic; using system.componentmodel; using system.linq; using system.text; using system.threading.tasks; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; using system.windows.threading; namespace wpfapplication6 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { private container _container; public mainwindow() { initializecomponent(); _container = new container(); } private void button_click(object sender, routedeventargs e) { dg.itemssource = _container.getitems(); } } public class dummy { public dummy() { _done=false; } private bool _done; public bool done { { homecoming _done; } // set trigger remotely too. set { _done = value; } } public void poll() { _done=!_done;} } public class item : inotifypropertychanged { public event propertychangedeventhandler propertychanged; private dummy dummy; public item(dummy dum) { dummy = dum; } public bool done { { homecoming dummy.done; } set { dummy.done = value; notify(); } } public void notify() { var handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs("done")); } } public class container { private list<dummy> dummies; private dispatchertimer _updatetimer; public container() { dummies = (from in enumerable.range(0, 10) select new dummy()).tolist(); } private ienumerable<item> items; public item[] getitems() { if (_updatetimer != null) _updatetimer.stop(); items=dummies.select(x => new item(x)); _updatetimer = new dispatchertimer(); _updatetimer.interval = timespan.fromseconds(1); _updatetimer.tick += (_1, _2) => { foreach (var item in dummies) { item.poll(); } foreach (var item in items) { item.notify(); } }; _updatetimer.start(); homecoming items.toarray(); } } }

c# wpf

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -