不包含' GetValue'的定义

时间:2015-07-22 06:11:22

标签: c# wpf c#-4.0 wpf-controls dependency-properties

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

错误:

  

不包含' GetValue'的定义没有扩展方法   '的GetValue'接受第一个类型的参数可以找到(是你   缺少using指令或程序集引用?)

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

2 个答案:

答案 0 :(得分:1)

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

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

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

您可以使用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);
            }

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

答案 1 :(得分:0)

您的类必须继承自DependencyObject,只是碰巧该类定义:

public class RadWindowProperties : DependencyObject