不包含'GetValue'的定义

时间:2021-07-20 13:28:12

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:

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);
           }
       }
    }
}

1 个解决方案

#1


1  

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

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

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 DependencyObject class before you can use GetValue method.

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

#1


1  

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

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

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 DependencyObject class before you can use GetValue method.

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