wpf 不包含“GetValue”的定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31555181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:17:22  来源:igfitidea点击:

does not contain a definition for 'GetValue'

c#wpfc#-4.0wpf-controlsdependency-properties

提问by user2408987

I am getting an error in the following dependency property

我在以下依赖项属性中遇到错误

Error:

错误:

Does not contain a definition for 'GetValue' and no extension method 'GetValue' accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)

不包含“GetValue”的定义,并且找不到接受第一个类型参数的扩展方法“GetValue”(您是否缺少 using 指令或程序集引用?)

My DP:

我的DP:

using System;
using System.Linq;
using System.Windows;

namespace NameSpace
{
   public class RadWindowProperties
    {
       public static readonly DependencyProperty ScreenSubtitleProperty = DependencyProperty.Register("ScreenSubtitle", typeof(string), typeof(RadWindowProperties), new PropertyMetadata(String.Empty));

       public string ScreenSubtitle
       {
           get
           {
               return (string)this.GetValue(ScreenSubtitleProperty);
           }
           set
           {
               this.SetValue(ScreenSubtitleProperty, value);
           }
       }
    }
}

回答by shreesha

According to msdnGetValue method Returns the current effective value of a dependency property on a dependency object.

根据msdnGetValue 方法返回依赖对象上的依赖属性的当前有效值。

for example if you create a dependency property like this

例如,如果您创建这样的依赖属性

public static readonly DependencyProperty BoundPasswordProperty =
                DependencyProperty.RegisterAttached("BoundPassword", typeof(string), typeof(PasswordBoxAssistant), new PropertyMetadata(string.Empty, OnBoundPasswordChanged));

You can use GetValue method to get the value of particular instance of DependencyObject.see the below code

您可以使用 GetValue 方法来获取 DependencyObject 的特定实例的值。请参阅以下代码

    private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
                {
                    PasswordBox box = d as PasswordBox;
                    string strValue=GetBoundPassword(d);
    }

public static string GetBoundPassword(DependencyObject dp)
            {
                return (string)dp.GetValue(BoundPasswordProperty);
            }

so your class must inherit from DependencyObjectclass before you can use GetValue method.

所以你的类必须从DependencyObject类继承,然后才能使用 GetValue 方法。

回答by thewhiteambit

Your class must inherit from DependencyObject, just change the class definition:

您的类必须从 DependencyObject 继承,只需更改类定义:

public class RadWindowProperties : DependencyObject