MVVM Light and combobox

I am new to WPF and MVVM Light, I would appreciate it if you could help me :-)

I would like to know how to implement combo box with MVVM Light to do the following:

1) Select an item in the drop-down list

2) Based on the selected value, change the other text fields in the graphical interface.

Thank you for your help.

Romny

+4
source share
1 answer

Well:

View:

<ComboBox ItemsSource="{Binding SourceData}" SelectedItem="{Binding SelectedSourceData,Mode=TwoWay}"/> <TextBlock Text="{Binding SelectedDataInTextFormat}"/> 

ViewModel:

 public class ViewModel:ViewModelBase { public ObservableCollection<Foo> SourceData{get;set;} public Foo SelectedSourceData { get{return _selectedFoo;} set{_selectedFoo=value; RaisePropertyChanged("SelectedSourceData"); SelectedDataInTextFormat=Foo.ToString(); } public string SelectedDataInTextFormat { get{return _selectedDataInTextFormat;} set{_selectedDataInTextFormat=value; RaisePropertyChanged("SelectedDataInTextFormat"); } } 

Basically, so that your view model can get the updated selected item from the drop-down list, make sure that the SelectedItem binding is set to Mode = TwoWay. To make sure that you push data from the viewmodel into the view when a change in the viewmodel occurs, make sure you call the RaisePropertyChanged helper class for the property that you want to update in the view.

+6
source

All Articles