I have a chart control in XAML, and datapoints that are bind to the control.
The problem is when my ModelView changes the Collection of points the Chart control doesn’t get any notifications. I have tried dp, with ObservableCollection and INotifyPropertyChanged without any luck. I know that there is a difference between changing a field/property and making collection operations such as (add/remove/replace etc.) for the changes to propagate to the Chart control. But I haven’t got it to work. The change event is only triggered when I instance/reinstance the collection.
Does any have link to a working MVVM that works with collections?
Worth too know.
public class ObservableCollection : Collection, INotifyCollectionChanged, INotifyPropertyChanged
public static DependencyProperty WorkModelsProperty = DependencyProperty.Register("WorkModels", typeof(ObservableCollection), typeof(Chart),
new PropertyMetadata(new ObservableCollection { }, new PropertyChangedCallback(
(sender, args) =>
{
Debugger.Break(); //trigged only when collection got new instance
})));
public ObservableCollection WorkModels
{
get { return (ObservableCollection)GetValue(WorkModelsProperty); }
set { SetValue(WorkModelsProperty, value); }
}
The binding is correct and tested.
Code in Window.Resources.
ObjectDataProvider ObjectType="{x:Type vm:ListWorkViewModel}" x:Key="ListWorkViewModel"
The binding of control.
WorkModels="{Binding Source={StaticResource ListWorkViewModel}, Path=WorkModels}"
In the ViewModel I use the following code to rise changes. (When using INotifyPropertyChanged)
WorkModels.Add(workModel);
this.RaisePropertyChanged("WorkModels");
protected void RaisePropertyChanged(string propertyName)
{
VerifyPropertyName(propertyName);
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
When I use ObservableCollection I only add new data point to the collection.
WorkModels.Add(workModel);
Thank you for answers. I found the solution to the problem here.
http://msdn.microsoft.com/en-us/library/aa970563.aspx