0

I have a view with a large number of ComboBox controls which can be populated with a Yes/No option.

I use this to enter some data into the database.

At the moment, to bind all of these combobox selected items I have to do this boilerplate code in my ViewModel for every combobox SelectedItem:

private string _YesNo1;
public string YesNo1
{
    get { return _YesNo1; }
    set
    {
        _YesNo1= value;
        RaisePropertyChanged("YesNo1");
    }
}

With my view I'd need to do this code some 15+ times and bind each one in XAML.

Is there a better way I can do this so I can bind each combobox SelectedItem separately but avoid this repeated code?

WSC
  • 903
  • 10
  • 30
  • This sounds like something that'd be more suited for [CodeReview](https://codereview.stackexchange.com/), since it's working code that you would like to improve. – Danny Goodall Feb 28 '20 at 14:05
  • Be careful with solutions that are “too clever” (like looping a number of controls). While it might save you a bit of time in the beginning, you risk ending up with something that’s difficult to both understand and change... – Jakob Busk Sørensen Feb 28 '20 at 14:18
  • @Noceo A good tip. Ordinarily I'd be happy writing longer but clearer code but in this case, it's just a lot of repetition. – WSC Feb 28 '20 at 14:21

1 Answers1

1

So there are few options.

  1. Make a base class which will implement INotifyPropertyChanged (https://stackoverflow.com/a/36151255/8212196). It will reduce a little bit code.
  2. There is a library Fody which has a plugin PropertyChanged.Fody. It generates code during compilation, which adds code related with INotifyPropoertyChanged.
Lukasz Szczygielek
  • 2,768
  • 2
  • 20
  • 34