1

クラスライブラリにある「FocusBehaviour」というクラスに1つの依存関係プロパティを作成しようとしています..以下のような名前空間を追加しました..しかし、1行目にエラーが表示されます)

エラー: System.windows.Controls.Control には、System.windows.Controls.Control 型の最初の引数を受け入れるメソッド 'GetValue" がありません。 2) SetValue() からも同じエラーが発生しています...その下... 3) Control control = dpObj as Control; エラー: System.DependencyObject を System.windows.Controls.Control に変換できません

WindowsBaseリファレンスも追加しました..

  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 プロジェクトで依存関係プロパティを作成しました。うまくいきましたが、別のクラスライブラリで作成すると、このエラーが表示されます。

これらのエラーが発生する理由 そして、このコードをどのように記述すればよいでしょうか?

4

2 に答える 2

2

からクラスを派生させるDependencyObject

public static class FocusBehavior : DependencyObject
{
    ...
}
于 2012-05-23T19:53:31.137 に答える
1

WindowsBaseアセンブリへの参照を追加する必要があります。DependencyObjectはそこにあります。

于 2012-05-23T06:36:23.707 に答える