I have 2 properties in ViewModel class, EmployeeList and Employee. EmployeeList is dynamic property, that being said, this property can change at run time. ViewModel class source code is below:
public class SampleViewModel
{
public ObservableCollection<Employee> EmployeeList { get; set; }
public Employee { get { ... } set { ... } }
}
View has set its ItemsSource and SelectedValue property to EmployeeList and Employee respectively.
ItemsSource="{Binding EmployeeList, Mode=TwoWay}"
SelectedValue="{Binding Employee, Mode=TwoWay}"
Everythings work well except EmployeeList has changed and Employee does not exist in EmployeeList, step of scenarios is below.
- EmployeeList has 2 employee, Mr. A and Mr. B and SelectedValue is Mr. A
- EmployeeList has changed to Mr. B and Mr. C, after this time SelectedValue is not work any more. If I set Employee on ViewModel and NotifyPropertyChanged this value won't update on UI or if I selected new Employee from UI this value won't update on ViewModel, the bottom line is View and ViewModel was disconnected since EmployeeList has changed and Employee does not exist in that list.
I want to know how can I fixed these problems?