.net - 依赖属性错误

标签 .net wpf dependency-properties

我正在学习依赖属性。我读了很多帖子和书籍,但仍然不清楚。

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

  • 自定义 Dependency 属性元素的主要用途是用于更改通知?
  • 我找到了 的“IsDefaultProperty”代码按钮 在 WPF 教科书中。这意味着' IsDefault ' 属性是依赖属性吗?
  • 为什么他们显示该代码?这意味着,在内部,在 Button 类中,它是这样定义的吗?
    (他们展示了内部代码?)或者他们展示了如何定义为自定义?

  • 这是我的代码:
    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/

    相关文章:

    c# - 所有 WPF 控件属性都是依赖属性。对或错?

    .net - 无法在 Visual Studio 2012 Ultimate 上安装 SSH.NET

    wpf - 如何在Expander的右上角添加按钮 - WPF DataGrid

    .net - 没有 MembershipUser 的自定义 MembershipProvider

    c# - 如何验证多个对象属性,然后在我的表单元素之一上应用带有工具提示的样式?

    c# - 图像更改源时的 WPF 幻灯片转换(这甚至可能吗?)

    c# - wpf - 更新的依赖属性不触发绑定(bind)

    c# - 将 Action 注入(inject) UserControl

    c# - 快速找到该对象的方法已更新

    javascript - 使用 Ajax 填充 Gridview