View only properties (eg: IsSelected) and the Model in MVVM View only properties (eg: IsSelected) and the Model in MVVM wpf wpf

View only properties (eg: IsSelected) and the Model in MVVM


Create a reusable Generic SelectableItem that wraps each item in the EmployeeList:

Simple example:

public class SelectableItem<T>: INotifyPropertyChanged{    public bool IsSelected {get;set;} //PropertyChanged(), etc    public T Value {get;set;}}

then in the ViewModel:

public ObservableCollection<SelectableItem<Employee>> Employees {get;set;}

and in the View:

<DataTemplate>   <CheckBox IsChecked="{Binding IsSelected}" Content="{Value.FullName}"/></DataTemplate>

Then you can retrieve all selected employees just by:

var selectedemployees = Employees.Where(x => x.IsSelected).Select(x => x.Value);