我想添加我自己的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
}
所以在我的虛擬機,我有一個屬性是ObserverableCollection。我理解你說的大部分內容,但是當我在虛擬機初始化時OnGraphsCollectionChanged沒有被調用時,我將新的GraphViewModel添加到集合中。只有在虛擬機啓動之後。你能提供一個我需要做的事情來解決的小例子嗎?對於這一切,我很新。謝謝! –
Nicros
2010-01-27 16:48:30
作了一些改動,看看 – 2010-01-27 23:35:26