c# - ComboBox.SourceUpdated event is not fired -


i have 2 comboboxes on view. both of them bound 2 different observablecollections in viewmodel, , when selected item in combobox1 changed, combobox2 gets updated different collection. binding works fine, however, want second combobox select first item in collection. initially, works, however, when source , items in combobox2 updated, selection index gets changed -1 (i.e. first item no longer selected).

to fix this, added sourceupdated event combobox2 , method event calls changes index 0. problem method never called (i put breakpoint @ top of method , doesn't hit). here's xaml code:

<grid>     <stackpanel datacontext="{staticresource mainmodel}" orientation="vertical">         <combobox itemssource="{binding path=fieldlist}" displaymemberpath="fieldname"                   issynchronizedwithcurrentitem="true"/>          <combobox name="cmbselector" margin="0,10,0,0"                   itemssource="{binding path=currentselectorlist, notifyonsourceupdated=true}"                   sourceupdated="cmbselector_sourceupdated">         </combobox>         </stackpanel> </grid> 

and in code-behind:

// never gets called private void cmbselector_sourceupdated(object sender, datatransfereventargs e) {     if (cmbselector.hasitems)     {         cmbselector.selectedindex = 0;     } } 

any appreciated.

after working on hour figured out. answer based on question: listen changes of dependency property.

so can define "property changed" event dependencyproperty on object. can extremely helpful when need extend or add additional events control without having create new type. basic procedure this:

dependencypropertydescriptor descriptor =     dependencypropertydescriptor.fromproperty(combobox.itemssourceproperty, typeof(combobox));  descriptor.addvaluechanged(mycombobox, (sender, e) =>  {     mycombobox.selectedindex = 0; }); 

what creates dependencypropertydescriptor object combobox.itemssource property, , can use descriptor register event control of type. in case, every time itemssource property of mycombobox changed, selectedindex property set 0 (which means first item in list selected.)


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -