我想创建一个从标准网格派生的自定义控件。 我添加了一个ObservableCollection作为自定义控件的DependencyProperty。但是,它的获取/设置从未达到。我可以在创建与ObservableCollection一起正确使用的DependencyProperty方面有一些指导吗?DependencyProperty getter/setter未被调用
public class MyGrid : Grid
{
public ObservableCollection<string> Items
{
get
{
return (ObservableCollection<string>)GetValue(ItemsProperty);
}
set
{
SetValue(ItemsProperty, value);
}
}
public static DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(ObservableCollection<string>),
typeof(MyGrid), new UIPropertyMetadata(null, OnItemsChanged));
}
observablecollection应该在您的viewmodel中,而不是您的控件... – thumbmunkeys 2012-02-03 10:15:31
它已经在我的viewmodel中。但是,我如何将它传递给网格?网格不应该知道如何处理集合吗? – phm 2012-02-03 10:18:07
您可以将依赖项属性定义为IEnumerable,如[ItemsControl.ItemsSource](http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource.aspx)。或者发布你的代码,这样有人可能会发现它有什么问题。 – Clemens 2012-02-03 10:27:24