2010-01-26 44 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