Selecting a Modified Event Using MVVM

I have a list containing a list of people. When the user clicks on an item, viewModel must set the currentPerson object to the object that the user clicked on.

I need to use ViewModel for this, so there is no code inside MainWindow.xaml.xs . Any ideas how to solve this?

+6
source share
1 answer

It is very simple:

Add the CurrentPerson property to your ViewModel and bind it to the SelectedItem ListBox property.

Something like that:

View Model:

 public Person CurrentPerson { get { return _currentPerson; } set { if(value == _currentPerson) return; _currentPerson = value; NotifyOfPropertyChange("CurrentPerson"); } } 

View:

 <ListBox SelectedItem="{Binding CurrentPerson}" ...> 
+6
source

Source: https://habr.com/ru/post/926954/


All Articles