我正在尝试在类库中名为“FocusBehaviour”的类中创建一个依赖属性。我添加了如下所示的命名空间。但它在第 1 行显示错误)return (bool)control.GetValue(FocusFirstProperty);
错误: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 项目中创建了一个依赖属性。它工作正常,但是当我在另一个类库中创建一个时,它会显示此错误。
为什么会出现这些错误?我应该如何编写这段代码?