Hi guys, I'm trying to bind focus for my control to a property on the view model, like this:
public class Focus
{
public static readonly DependencyProperty HasFocusProperty = DependencyProperty.RegisterAttached("HasFocus",
typeof(bool),
typeof(Focus),
new PropertyMetadata(false, HandleHasFocusChanged),
null
);
private static void HandleHasFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uiElement = d as UIElement;
var value = (bool)e.NewValue;
if (value)
{
FocusManager.SetFocusedElement(uiElement, uiElement);
}
}
public static bool GetHasFocus(UIElement obj)
{
return (bool)obj.GetValue(HasFocusProperty);
}
public static void SetHasFocus(UIElement obj, bool value)
{
obj.SetValue(HasFocusProperty, value);
}
}
This works for the first focus, but then after that it seems to not have any affect at all
Anybody know what I'm doing wrong, or even a better way to do what I'm trying to achieve?