c# - Can't get change notification from Collection to UI control bound to a LINQ query -


i'm trying solve simple problem using linq, i'm learning.

i have collection of strings, in case representing serial ports, displayed in control, must ordered. original collection unsorted, , don't want modify it, or make copy of it. so, created property of type ienumerable , bound combobox.

this works great, combobox has correct contents in correct order. however, if original collection changes, either a) combobox doesn't notified when original collection changes, or b) linq query isn't being refreshed.

after trying different things, can't see how following code doesn't work. must missing something.

there might code here redundant... anyway, source followed xaml controls:

public partial class mainwindow : window {     observablecollection<string> original = new observablecollection<string>();     public observablecollection<string> originallist {         { return (original); }     }      private ienumerable<string> _portlist;     public ienumerable<string> portlist {         { return (_portlist); }     }      public mainwindow() {         initializecomponent();          original.add("com5");         original.add("com1");         original.add("com3");         original.add("com4");         original.add("com2");          original.collectionchanged += new notifycollectionchangedeventhandler(originalchanged);          _portlist = (             port in original             orderby port ascending             select port         );          datacontext = this;     }      private void addportbutton_click(object sender, routedeventargs e) {         original.add("com2.5");     }      void originalchanged(object sender, notifycollectionchangedeventargs args) {         notifypropertychanged("portlist");     }      public event propertychangedeventhandler propertychanged;     private void notifypropertychanged(string info) {         if (propertychanged != null) {             propertychanged(this, new propertychangedeventargs(info));         }     } } 

the xaml:

    <combobox name="serialportlistbox" itemssource="{binding portlist}" width="100" />     <combobox grid.row="1" name="originallistbox" itemssource="{binding originallist}" width="100" margin="0,5,0,0"/>     <button grid.column="1" name="addportbutton" content="add port 2.5" width="100" margin="10,0,0,0" click="addportbutton_click" /> 

i think change notifications want binding combobox observablecollection. class implements interface inotifycollectionchanged, innards of wpf rely on notify ui updates required.

i see trying around implementing inotifypropertychanged portlist, won't work way want. interface not trigger appropriate event trigger combobox refresh. inotifycollectionchanged tells listener collection has changes (i.e; 'add', 'remove', 'move', 'replace', 'reset'), whereas inotifypropertychanged indicates value has changed in bound object. combobox not respond inotifypropertychanged event, in fact it's not subscribing events of type.

so, either bind directly underlying datasource, or implement 2nd observablecollection on top of that, rather ienumerable, not notify on change.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -