次のように、コントロールのフォーカスをビューモデルのプロパティにバインドしようとしています。
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);
}
}
これは最初のフォーカスでは機能しますが、その後はまったく影響がないようです。
誰かが私が間違っていること、または私が達成しようとしていることを行うためのより良い方法を知っていますか?