I have a class, "MainViewModel" that includes an Observable Collection of ColourMappingViewModel, and references out to two other ViewModels, one which is used to update the ColourMappingViewModels (ColourControllerViewModel), and one which contains a CollectionView which is used to display a particular subset of these ColourMappingViewModels (ColourSelectionViewModel). Both these classes are passed the ObservableCollection to bind to.
ColourMappingViewModel has two properties: The Color , and the Type.
public class ColourMappingViewModel : ViewModel,
{
public Color TextColour
{
get
{
return (Color)ColorConverter.ConvertFromString(_colourMapping.TextColour);
}
set
{
_colourMapping.TextColour = value.ToString().Remove(1, 2);
OnPropertyChanged();
}
}
public ColourMappingUsageType Type
{
get
{
return _colourMapping.Usage;
}
set
{
_colourMapping.Usage = value;
OnPropertyChanged()
}
}
}
ColourController includes a DataGrid, with the Type column including a ComboBox which binds to a collection of the enum types and the "Type" property like so:
ItemsSource="{Binding Types, Mode=OneWay}"
SelectedValue="{Binding Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
And similarly, another ComboBox containing the ColourList, bound like so:
ItemsSource="{Binding DataContext.ColoursList,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type view:ColourController}}}"
SelectedItem="{Binding BackgroundColour, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
The CollectionView on ColourSelectionViewModel filters the list based on the value of "Type", and I have another ComboBox on my ColourSelection view that is bound to this CollectionView.
Now. If I update the "TextColour" of in the ColourController, this is reflected in the ColourSelector - I can see the changes.
If I update the 'Type', though, the whole thing falls over with a StackOverflowException due to "OnPropertyChanged()" being repeatedly called. My question is, why!?
Update your code to: