依赖属性错误 [英] Dependency property error

查看:110
本文介绍了依赖属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习依赖项属性。我读了很多文章书,但我仍然不清楚。

I am learning dependency properties. I read many posts & books but still I am not clear.

下面显示的程序是我写的要学习的程序。出现一些错误,请帮助解决。我有问题。

The program shown below is which I wrote to learn. Some error in that, please help in resolving. I have questions.


  1. 自定义Dependency属性元素的主要用途是用于更改通知?

  2. 我在WPF教科书中为 Button 找到了一个'IsDefaultProperty'代码。这意味着 IsDefault 属性是依赖项属性吗?

  3. 为什么他们显示该代码?在内部,这意味着它在Button类中的定义是这样的?
    (他们显示了内部代码?)还是显示了如何定义为自定义?

  1. The main use of custom Dependency property element is for the notification of change?
  2. I found an 'IsDefaultProperty' code for Button in a WPF text book. It means 'IsDefault' property is a dependency property?
  3. 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?

这是我的代码:

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


推荐答案


自定义Dependency属性元素的主要用途是用于
更改通知?

The main use of custom Dependency property element is for the notification of change?

不,也可以通过让类实现 INotifyPropertyChanged 来安排。依赖项属性可提供更改通知,但实际原理有所不同。请参阅为什么依赖属性? WPF财产制度如何经济?

No, that can also be arranged by having the class implement INotifyPropertyChanged as well. Dependency properties to provide change notifications, but the real rationale is something different. See Why dependency properties? and How is the WPF property system economical?


我在WPF中为Button找到了 IsDefaultProperty代码课本。
表示'IsDefault'属性是依赖项属性吗?

I found an 'IsDefaultProperty' code for Button in a WPF text book. It means 'IsDefault' property is a dependency property?

是。命名字段 FooBarProperty是用于定义依赖项属性的WPF约定。您可以检查 IsDefaultProperty 来查看它确实是一个依赖项属性,甚至是 IsDefault 文档的一节称为依赖项属性信息。

Yes. Naming a field "FooBarProperty" is the WPF convention for defining dependency properties; you can check the docs for IsDefaultProperty to see that it's indeed a dependency property, and even the IsDefault documentation has a section called "dependency property information".


为什么他们显示该代码?这意味着在内部,在Button类中,其
是这样定义的吗? (他们显示了内部代码?)还是他们显示了如何将
定义为自定义?

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 中那样(几乎是因为我不确定您指的是什么)。

I 'm not sure what "that" code is, but yes, the property is almost certainly defined like that in Button ("almost" because I 'm not sure what you are referring to).


错误:在当前上下文中不存在GetValue和SetValue

Error: GetValue and SetValue does not exist in the current context

那是因为您不是从 DependencyObject

That's because you are not deriving from DependencyObject.

这篇关于依赖属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆