Whenever the ItemsSource of a WPF ComboBox changes (i.e. adding or removing elements, or assigning a new Collection), I want it to do the following (pseudo code):
IsEnabled = Items.Count > 1;
if (Items.Count == 1)
SelectedItem = Items.First();
I'm using MVVM, and I know how to do it in MVVM. But I want to avoid the boilerplate code, and IMHO it is too UI-specific for the ViewModel to care about. So is there a way to specify this in a Behavior<ComboBox>?
I couldn't find a ComboBox event I could subscribe to. If possible, it should work on ObservableCollection as well as ICollectionView or List etc.
You could use a
Stylewith triggers:If you for example put this in your
App.xamlfile, you can use it across the whole application, e.g.: