Open ComboBox from ViewModel in MVVM-scenario

Open ComboBox from ViewModel in MVVM-scenario

On the Microsoft MSDN-Forums there was a question how to open up a Combobox in an MVVM-scenario from the ViewModel.

To do this, you use the typical MVVM way: You define a bool-Property in the ViewModel to control the DropDown. Here I named it IsDropDownOpen:

Note that I’m using a ViewModelBase-class in the snippet above. That ViewModelBase-class defines the OnPropertyChanged-method to raise the PropertyChanged-event:

Now let’s assign the MainViewModel to the MainWindow’s DataContext:

Now in XAML I can bind the ComboBox’ IsDropDownOpen-Property to the IsDropDownOpen-Property of the MainViewModel that is in the DataContext. In addition I bound a Checkbox to the IsDropDownOpen-Property of the MainViewModel to be able to change the value of that property:

That’s it. When I check the CheckBox, the Dropdown-List opens. Instead of changing the IsDropDownOpen-Property of the MainViewModel with a CheckBox, you could also set the IsDropDownOpen-Property in the MainViewModel when for example a Command is executed.

Share this post