SHR*_*HRI 7 .net wpf dependency-properties
我正在学习依赖属性.我读过很多帖子和书籍,但我还不清楚.
下面显示的程序是我写的要学习的.有些错误,请帮忙解决.我有疑问
这是我的代码:
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)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我看到了错误:
> Error: GetValue and SetValue does not exist in the current context
Run Code Online (Sandbox Code Playgroud)
Jon*_*Jon 16
自定义Dependency属性元素的主要用途是用于更改通知吗?
不,这也可以通过具有类实现来安排INotifyPropertyChanged.依赖属性提供更改通知,但真正的基本原理是不同的.查看为什么依赖属性?而如何为WPF属性系统经济?
我在WPF教科书中找到了Button的'IsDefaultProperty'代码.这意味着'IsDefault'属性是依赖属性?
是.命名字段"FooBarProperty"是用于定义依赖项属性的WPF约定; 您可以检查文档IsDefaultProperty以查看它确实是一个依赖属性,甚至IsDefault文档中也有一个名为"依赖属性信息"的部分.
为什么他们展示代码?这意味着,在内部,在Button类中,它的定义是什么?(他们展示了内部代码?)或者他们展示了如何定义自定义?
我不确定那个"那个"代码是什么,但是,是的,该属性几乎肯定被定义为Button("几乎"因为我不确定你指的是什么).
错误:当前上下文中不存在GetValue和SetValue
那是因为你不是从中衍生出来的DependencyObject.
Dre*_*kes 14
DependencyProperty必须在实例上定义实例DependencyObject.因此,您的类必须派生自DependencyObject或其子类之一.WPF中的许多类型都源于此,包括Button.
因此,要在这种情况下使代码正常工作,您必须使用:
public class Contact : DependencyObject
{
// ...
Run Code Online (Sandbox Code Playgroud)
这就是为什么你在收到错误GetValue和SetValue-它们被定义DependencyObject.
| 归档时间: |
|
| 查看次数: |
13504 次 |
| 最近记录: |