2012-05-23 52 views
1

i am trying to create one dependency property in a class called "FocusBehaviour" that is in Class library.. I have added the namespaces like below.. But it showing error in line 1) return (bool)control.GetValue(FocusFirstProperty);爲什麼我不能在一些類庫

error: System.windows.Controls.Control Doesn't have a method 'GetValue" that accepts the First argument of type System.windows.Controls.Control. 2) The same error is coming from SetValue() also...below that... 3) Control control = dpObj as Control; error:Cann't convert System.DependencyObject To System.windows.Controls.Control

I Also Added the WindowsBase Reference..

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 

namespace MyOrgBI 
{ 
    public static class FocusBehavior:DependencyObject 
    { 

     public static readonly DependencyProperty FocusFirstProperty = 
     DependencyProperty.RegisterAttached("FocusFirst", 
              typeof(bool), 
              typeof(Control), 
              new PropertyMetadata(OnFocusFirstPropertyChanged)); 

    public static bool GetFocusFirst(Control control) 
    { 
     return (bool)control.GetValue(FocusFirstProperty); 
    } 
    public static void SetFocusFirst(Control control, bool value) 
    { 
     control.SetValue(FocusFirstProperty, value); 
    } 
    static void OnFocusFirstPropertyChanged(DependencyObject dpObj, DependencyPropertyChangedEventArgs args) 
    { 
     Control control = dpObj as Control; 
     if (control == null || !(args.NewValue is bool)) 
     { 
      return; 
     } 
     if ((bool)args.NewValue) 
     { 
      control.Loaded += (sender, e) => control.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); 
     } 
    } 
} 

}創建另一個類的依賴歡迎使用屬性

以前我在一個WpfApplication項目中創建一個依賴屬性。它工作正常,但是當我在另一個類庫中創建一個時,它顯示此錯誤。

爲什麼會出現這些錯誤?我該如何編寫這段代碼?

回答

1

您需要添加對WindowsBase裝配的引用。 DependencyObject位於那裏。

+0

即使我添加了「WindowsBase」從引用AddReference選項派生類,它並沒有關閉錯誤... –

2

DependencyObject

public static class FocusBehavior : DependencyObject 
{ 
    ... 
} 
相關問題