2010-01-26 26 views
0

我想添加我自己的ItemsSource来提供GraphViewModels列表到图表。我不认为我有它很正确,但当我创建我的第一个GraphViewModel并将其添加到图形,我的DP更新,但OnGraphsCollectionChanged不被调用。依赖属性和ViewModel初始化问题

这应该如何工作?如果我通过绑定到命令的按钮将图形添加到我的VM属性,那么一切都很好。

这里是DP代码,任何人都可以解释这是应该如何工作,或者什么我做错了在初始化过程中显示我的数据?

public static readonly DependencyProperty GraphsProperty = 
    DependencyProperty.Register("ItemsSource", 
    typeof(ObservableCollection<GraphViewModel>), 
    typeof(DynamicPlotter), 
    new FrameworkPropertyMetadata(new PropertyChangedCallback(ChangeGraphs))); 

public ObservableCollection<GraphViewModel> ItemsSource 
{ 
    get { return (ObservableCollection<GraphViewModel>)GetValue(GraphsProperty); } 
    set 
    { 
     SetValue(GraphsProperty, value); 
     ItemsSource.CollectionChanged += new NotifyCollectionChangedEventHandler(OnGraphsCollectionChanged); 
    } 
} 

public static void ChangeGraphs(DependencyObject source, DependencyPropertyChangedEventArgs eventArgs) 
{ 
    (source as DynamicPlotter).UpdateGraphs((ObservableCollection<GraphViewModel>)eventArgs.NewValue); 
} 

private void UpdateLineGraphs(ObservableCollection<GraphViewModel> grphs) 
{ 
    this.ItemsSource = grphs; 
} 

private void OnGraphsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
{ 
    // This never gets called when you set the ItemsSource, but is where all the work is done 
} 

回答

1

CollectionChanged只有被调用时收集的变化,如果集合已经被设置你将永远不会得到您的通知,直到有被添加/删除之前填充。其次,如果你正在从xaml设置依赖属性,getter/setter就不会被使用,依赖机制使用它自己的内部setter例程。您应该将您的collectionChanged事件附加到您的ChangeGraphs属性回调函数中,因为每次设置/更改属性时都会调用它。您也可以使用它来解除旧的collectionChanged事件,事件参数将为您提供旧的和新的值。

但实际上,它是一个可观察的集合,您不应该知道集合何时发生更改,因为您应该绑定到集合,并且在更改绑定机制时会更新您的UI。

我会改变我的代码看起来像这样

public ObservableCollection<GraphViewModel> ItemsSource { 
    get { return (ObservableCollection<GraphViewModel>)GetValue(ItemsSourceProperty); } 
    set { SetValue(ItemsSourceProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ItemsSourceProperty = 
     DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<GraphViewModel>), typeof(DynamicPlotter), new UIPropertyMetadata(null, (o, e) => { ((DynamicPlotter)o).ItemsSourceChanged(); })); 

    private void ItemsSourceChanged() { 
    if (this.ItemsSource != null){ 
     //attach the collection changed listener, this will listen to all FUTURE collection changes, items that are added and removed 
     this.ItemsSource.CollectionChanged +=new NotifyCollectionChangedEventHandler(ItemsSource_CollectionChanged); 
     //do some inital processing with the items that are in the collection already when it is set 
     this.UpdateGraphs(this.ItemsSource); 
    } 

    private void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e){ 
      //this will get called if an item gets added or removed from the collection 
    } 
+0

所以在我的虚拟机,我有一个属性是ObserverableCollection 。我理解你说的大部分内容,但是当我在虚拟机初始化时OnGraphsCollectionChanged没有被调用时,我将新的GraphViewModel添加到集合中。只有在虚拟机启动之后。你能提供一个我需要做的事情来解决的小例子吗?对于这一切,我很新。谢谢! – Nicros 2010-01-27 16:48:30

+0

作了一些改动,看看 – 2010-01-27 23:35:26