Hello. I'm currently learning WPF and MVVM. Recently, I have a question that I have not been able to find the answer. The essence of the problem is as follows:
I have a window in which the ComboBox is present. In it, the user selects the operation he needs. ComboBox values ββare enum Operaton elements that are bound by a binding to an ItemsSource. There is also a ListBox displaying user-selected objects and several Button. Depending on the operations selected in the ComboBox, the objects displayed in the ListBox and the commands executed by some buttons change. At the same time, the View remains the same.
I created my ViewModel for each selected operation and placed them as public properties in the main MainViewModel. For the main window, set DataContext = MainViewModel. For ListBox and buttons that change the behavior of DataContext = MainViewModel.FirstOperationViewModel. But how to change this DataContext when the operation selected in the ComboBox changes? I come up with two options:
- Modify with an event handler in code behind, violating the idea of ββMVVM.
- Make the binding DataContext of the controls I need and the SelectedItem of my ComboBox using the ValueConverter. This option seems to me quite doubtful.
In this regard, I have questions:
- How to correctly implement this behavior in the framework of MVVM?
- Does it make sense in this case to stick to MVVM? Because In some cases, as I understand it, a departure from this model makes sense.
- Maybe use some other approach to solve this problem? If so, then at least briefly describe it.
Thank.