2015-07-22 65 views
-2

,我在下面的依賴屬性得到一個錯誤不包含 '的GetValue'

錯誤的定義:

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?)

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

那麼你的'RadWindowProperties'類似乎不是來自任何東西 - 你期望它從哪裏得到一個'GetValue'方法? –

+0

'GetValue'在'DependencyObject'類中定義。 –

+0

是的,我想獲得一個GetValue,所以如果我繼承的DependencyObject是好的?我是WPF的新手。 – user2408987

回答

1

根據msdn GetValue方法返回依賴項對象的依賴項屬性的當前有效值。

例如,如果您創建一個依賴屬性像這樣

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

您可以使用GetValue方法來獲得DependencyObject.see的特定實例下面的代碼

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方法。

+0

我的回答有什麼問題? – shreesha