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.ControlI 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));
}
}
}
}
Previously I created a dependency property in a WpfApplication project. It worked fine but when I create one in another class library it shows this error.
Why these errors Coming ? And how should I write this code?
You need to add a reference to the
WindowsBaseAssembly. DependencyObject is located there.