那么问题应该是它不编译。由于ObservableCollection<T>
不是从 派生的DependencyObject
,因此即使我会更正其余代码,此实现也无法工作,这也是完全错误的。
有关更多信息,DependencyProperties
请查看此处。
编辑
您的财产的正确实施将是
public class SomeClass : DependencyObject
{
public static readonly DependencyProperty UrlProperty =
DependencyProperty.Register("Url", typeof(Uri), typeof(SomeClass));
public Uri Url
{
get { return (Uri)GetValue(UrlProperty);}
set { SetValue(UrlProperty, value); }
}
}
编辑 2
包装器实现
public class SomeClass : DependencyObject
{
public static readonly DependencyProperty UrlProperty =
DependencyProperty.Register("Url", typeof(Uri), typeof(SomeClass),
new PropertyMeta(OnUrlChanged));
public Uri Url
{
get { return (Uri)GetValue(UrlProperty);}
set { SetValue(UrlProperty, value); }
}
public ObservableCollection<Song> Songs { get; set; }
private static void OnUrlChanged (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var wrapper = d as SomeClass;
if (wrapper == null)
return;
// ... what ever you want to do with the collection
}
}