I am new to wpf and MVVM, and I've spent all day trying to get the value of a ComboBox to my ViewModel on SelectionChanged. I want to call a function in the selection changed process. In mvvm, what is the solution for it?
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
In MVVM, we generally don't handle events, as it is not so good using UI code in view models. Instead of using events such as
SelectionChanged
, we often use a property to bind to theComboBox.SelectedItem
:View model:
View:
Now whenever the selected item in the
ComboBox
is changed, so is theItem
property. Of course, you have to ensure that you have set theDataContext
of the view to an instance of the view model to make this work. If you want to do something when the selected item is changed, you can do that in the property setter: