c# - Set the SelectionChanged event of a ComboBox while binding its SelectedItem and ItemsSource in XAML -


i'm trying set combobox options binded list of strings, default selected value binded setting, , event handler selection changed.

i want configure using xaml so:

    <combobox name="routescombobox"               itemssource="{binding routes}"               selecteditem="{binding defaultroute}"                selectionchanged="routefilter_selectionchanged" /> 

but when on startup throws error:

an unhandled exception of type 'system.reflection.targetinvocationexception' occurred in presentationframework.dll

if of in xaml, either set selectionchanged event or itemssource programatically in c# below works fine. have lot of these comboboxes rather straight in xaml.

<combobox name="routescombobox"           itemssource="{binding routes}"           selecteditem="{binding defaultroute}" /> 

with c#:

public ienumerable<string> routes {     { return lubricationdatabase.getroutes(); } }  public string defaultroute {     { return mysettings.default.defaultroute; }     set { } /* side question: without this, throws parse exception. idea why? */ }  public mainwindow() {      this.datacontext = this;      initializecomponent();       routescombobox.selectionchanged += routefilter_selectionchanged;  } 

i've tried solution found here:

private string _defaultroute; public string defaultroute {     { return mysettings.default.defaultroute; }     set     {         if (_defaultroute != value)         {             _defaultroute = value;              // fires before `selectedvalue` has been              // updated, , handler function uses that,             // manually set here.             routescombobox.selectedvalue = value;             selectionchangedhandler();          }     } } 

which okay, pretty bulky , more work worth when can programatically assign selectionchanged event.

again if possible i'd using xaml because have lot of these comboboxes , initializing them in c# awful.

any ideas?

why binding selecteditem when you're not going update item when user changes selection? not sure event handler doing, have working solution way wanted it.

in short, need keep track of defaultroute using backing field. also, need notify ui when selected item changes in view model; way don't seem doing, mvvm. should hooking selection changed event if plan on updating view in way. other changes should handled in view models defaultroute setter

xaml

<combobox name="routescombobox"       itemssource="{binding routes}"       selecteditem="{binding defaultroute}"        selectionchanged="routefilter_selectionchanged" /> 

code

public partial class mainwindow : window, inotifypropertychanged {     public ienumerable<string> routes     {                 {             return new string[] { "a", "b", "c", "d" };         }     }      public string defaultroute     {                 {             return _defaultroute;         }         set         {             _defaultroute = value;             // handle saving/storing setting here, when selection has changed             //mysettings.default.defaultroute = value;             notifypropertychanged();         }      }      public mainwindow()     {         this.datacontext = this;         initializecomponent();          defaultroute = mysettings.default.defaultroute;     }      private string _defaultroute;      public event propertychangedeventhandler propertychanged;      private void notifypropertychanged([callermembername] string propertyname = "")     {         propertychanged?.invoke(this, new propertychangedeventargs(propertyname));     }      private void routefilter_selectionchanged(object sender, selectionchangedeventargs e)     {      } }  public static class mysettings {     public static class default     {         public static string defaultroute = "a";     } } 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -