我正在学习依赖属性。我读了很多帖子和书籍,但仍然不清楚。
下面显示的程序是我写来学习的。其中有一些错误,请帮助解决。我有问题。
(他们展示了内部代码?)或者他们展示了如何定义为自定义?
这是我的代码:
namespace DependencyProperties
{
public class Contact
{
private int id=100;
private string name="shri";
public static readonly DependencyProperty IsPresentProperty;
public int ID
{
get { return id; }
}
public string NAME
{
get { return name; }
}
static Contact()
{
IsPresentProperty = DependencyProperty.Register("IsPresent", typeof(bool),typeof(Contact),new FrameworkPropertyMetadata(false,new PropertyChangedCallback(OnIsPresentChanged)));
}
public bool Present
{
get { return (bool)GetValue(Contact.IsPresentProperty); }
set { SetValue(Contact.IsPresentProperty, value); }
}
private static void OnIsPresentChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
}
}
}
我看到错误:
> Error: GetValue and SetValue does not exist in the current context
最佳答案
The main use of custom Dependency property element is for the notification of change?
不,这也可以通过让类实现
INotifyPropertyChanged
来安排以及。提供更改通知的依赖属性,但真正的原理是不同的。见 Why dependency properties?和 How is the WPF property system economical?I found an 'IsDefaultProperty' code for Button in a WPF text book. It means 'IsDefault' property is a dependency property?
是的。将字段命名为“FooBarProperty”是用于定义依赖项属性的 WPF 约定;您可以查看
IsDefaultProperty
的文档看到它确实是一个依赖属性,甚至是 IsDefault
文档有一个名为“依赖属性信息”的部分。Why they shown that code? It means, internally, in Button class, its defined like that? (They showed internal code?) or they showed how to define as custom?
我不确定“那个”代码是什么,但是是的,几乎可以肯定该属性的定义与
Button
中的一样。 (“几乎”是因为我不确定您指的是什么)。Error: GetValue and SetValue does not exist in the current context
那是因为您不是源自
DependencyObject
.
关于.net - 依赖属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10512629/