c# - Elegant way of binding combobox in datagrid with a new reference of ObservableCollection -


i have combobox in datagrid i'm binding observable collection to.

xaml

<usercontrol x:class="ddcscheduler.view.ddcschedule" ... ... <datagrid x:name="scheduledatagrid"                    itemssource="{binding schedulecollection}"                   canuserresizecolumns="true"                    autogeneratecolumns="false"                   isreadonly="true">     <datagridtemplatecolumn header="holidays" width="auto">          <datagridtemplatecolumn.celltemplate>              <datatemplate>                  <combobox itemssource="{binding relativesource={relativesource                       findancestor, ancestortype={x:type my:ddcschedule}},                       path=datacontext.nationalholidaycollection}"                      displaymemberpath="holidaycategoryname"                      selectedvalue="{binding nationalholiday,                         updatesourcetrigger=propertychanged}">                   </combobox>               </datatemplate>            </datagridtemplatecolumn.celltemplate>       </datagridtemplatecolumn>    </datagrid.columns> </datagrid> 

code behind

public partial class ddcschedule : usercontrol, inotifypropertychanged {     private observablecollection<schedule> _schedulecollection =          new observablecollection<schedule>();     private observablecollection<nationalholiday> _nationalholidaycollection;      public observablecollection<nationalholiday> nationalholidaycollection     {         { return _nationalholidaycollection; }         set         {             if (equals(value, _nationalholidaycollection)) return;             _nationalholidaycollection = value;             onpropertychanged("nationalholidaycollection");         }     }      public observablecollection<schedule> schedulecollection     {         { return _schedulecollection; }         set         {             if (equals(value, _schedulecollection)) return;             _schedulecollection = value;             onpropertychanged("schedulecollection");         }     }      private void usercontrol_loaded(object sender, routedeventargs e)     {         if (_isscreenloaded)             return;         this.datacontext = this;         _isscreenloaded = true;     } 

which works fine. until introduced following:

    private void menuitem_settings_onclick(object sender, routedeventargs e)     {         settingsmodalwindow window = new settingsmodalwindow();         window.showdialog();          if (window.dialogresult != true)             return;          //assign newly created national holiday collection.         nationalholidaycollection = window.nationalholidaycollection;     } 

schedule.cs (model datagrid's itemsource)

public class schedule : inotifypropertychanged {     private nationalholiday _nationalholiday;      public nationalholiday nationalholiday     {         { return _nationalholiday; }         set         {             if (equals(value, _nationalholiday)) return;             _nationalholiday = value;             onpropertychanged("nationalholiday");         }     }     ... } 

the settingsmodalwindow produces new instance of observablecollection inside settings window. assign newly created observablecollection bound collection, selected values in comboboxes in datagrid gets cleared (but list updated newly bound collection).

this understandable behavior, since code assigning newly created observablecollection settings selectedvalue no longer referencing same object.

my question is, what's elegant way of solving this? it's not can change model value type rather reference type overcome this.

edit:

these screenshots shows problem. have 2 rows holiday a,b set in comboboxes.

enter image description here

then go settings, , add more holidays (observablecollection inside settings) show on comboboxes.

enter image description here

as press confirm, newly created observablecollection returned , assigned in usercontrol opened settings modal window. assignment done, every combobox cleared. (but when click on combobox, newly created collection bound)

enter image description here

there's no point in using observablecollection if overwrite whole, edit contents. 1 never provide setter on collection properties.

(implementation note: can record collectionchanged events in dialog , apply modifications original collection)


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -