I am going to simplify things to make everything clear as possible. Lets say I have current Model:
public class Product
{
public int Id {get;set;}
public string Name {get;set;}
public int GroupId {get;set;}
public Group Group {get;set;}
}
public class Group
{
public int Id {get;set;}
public string Name {get;set;}
}
And lets say that I have xaml similar to this: (Source is a public property of type Product exposed through ViewModel, GroupList is ObservableCollection of Group, also exposed through ViewModel):
<TextBlock Text="Id" />
<TextBox Grid.Column="1" Text="{Binding Source.Id}" />
<TextBlock Grid.Row="1" Text="Name" />
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Source.Name}" />
<TextBlock Grid.Row="2" Text="Group" />
<ComboBox Grid.Column="1" Grid.Row="2"
DisplayMemberPath="Name"
ItemsSource="{Binding GroupList}"
SelectedValue="{Binding Source.GroupId}"
SelectedValuePath="Id"
/>
This all works fine. However, imagine that for some reason I need to refresh items in GroupList. A ICommand on ViewModel is invoked, data is fetched, and GroupList is set to new list. It seems that refreshing GroupList has lead to resetting GroupId of the current Product, by setting it to NULL.
Why is this happening, and what is the best way to handle this, other than manually setting it to after the list was refreshed? The fact that GroupId property is being changed is not good, since there could be some actions on PropertyChanged for this property. For that reason I wold also need to introduce a private bool variable _isRefreshing Groups, so I can prevent some actions when they are not necessary at all, and this all "pollutes" code.