I'm writing WPF app which intend to solve standard problems. I'm really new to WPF and MVVM pattern, so there's a little mess in my head after reading tons of different approaches to MVVM on the internet. I'd like to know how my simple operation of DataGrid's itemsource refresh operation is "idiomatic" to MVVM.
Let's say I have one datagrid and one combobox. The combo contains list of all coaches. The datagrid shows you all sportsmen trained by selected coach so the combo acts like a filter for data in datagrid:
<ComboBox ItemsSource="{Binding ListCoach}" DisplayMemberPath="last_name" SelectedValue=
"{Binding SelectedCoach}" SelectedValuePath="Id"/>
<DataGrid ItemsSource="{Binding Path=ListSportsman}" ..... </DataGrid>
My ViewModel class change content of the DataGrid in the setter of SelectedCoach property (this property is a target for Combobox's value):
private int _selectedCoach;
public int SelectedCoach
{
get { return _selectedCoach; }
set
{
_selectedCoach = value;
ListSportsman = new ObservableCollection<sportsmanset>(_serviceAgent.ListSportsmanOfCoach(value));
NotifyPropertyChanged(vm => vm.SelectedCoach);
}
}
Doesn't such code smell? Or It would be more appropriate to subscribe for change in SelectedCoach property and set ListSportsman in separate function? (by the way, how to subscribe for NotifyPropertyChanged event manually?)
It is not wrong by definition but there is one thing to consider:
In general developers expect setters and getters to be fast. So if you are adding logic that takes a considerable amount of time, you might want to execute that logic asynchronously or replace the property with a Set method so it is clear that there is processing involved.
Part of the processing could be setting a property that the View could bind to.