I have an Objectmodel with some nested Collections:
public class MainViewModel : BaseViewModel
{
private Dictionary<String, Diagram> _diagrams;
public Dictionary<String, Diagram> Diagrams
{
get { return _diagrams; }
set
{
_diagrams = value;
OnPropertyChanged("Diagrams");
}
}
}
Base Viewmodel implements INotifyPropertyChanged. Inside Diagram there is a Collection of Curves:
public class Diagram
{
private ObservableCollection<DiagramCurve> curves;
[JsonIgnoreAttribute]
public ObservableCollection<DiagramCurve> Curves
{
get { return curves; }
}
public Diagram()
{
curves = new ObservableCollection<DiagramCurve>();
}
}
I bind to an instance of DiagramCanvas in my Xaml like this:
<wd:DiagramCanvas x:Name="diagramCanvas"
Diagram ="{Binding Diagrams[Diagram1]}"
Grid.Row="2" Grid.Column="1"
Height="auto" Width="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="10"
Background="{StaticResource DiagrambackgroundBrush }" />
which works fine if I assign the the Diagrams property of MainView a completely new Diagrams collection. But what I need is that the DiagramCanvas Control get’s updated when the Curves Collection changes. But this does not happen.
I tried to inherit
Diagramfrom an ‘ObservableCollection’, but even this didn’t help as in ObservableCollection dependency property does not update when item in collection is deleted explained.As the DependencyProperty of my CustomControl
DiagramCanvasbinds to a value item of the DictionaryMainViewModel.Diagramsnot to a property of aDiagraminstance, noOnPropertyChangedevent of the DependencyProperty ofDiagramCanvasis fired, when properties of theDiagraminstance change.Only when a new value is assigned to the value item inside the Dictionary
MainViewModel.Diagrams, this event gets fired, beacause this assigns a new value to theDependencyPropertyofDiagramCanvas.Therefore I have to subscribe to the events of the collection manually:.