我想添加我自己的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