【发布时间】:2023-01-07 09:29:24
【问题描述】:
在 MVVM 架构中设置控制焦点的好做法是什么。我试过 FocusBehavior 但它没有用。什么是最好的解决方案?
焦点行为.cs
public class FocusBehavior
{
public static void SetIsFocused(UIElement element, bool value)
{
element.SetValue(IsFocusedProperty, value);
}
public static bool GetIsFocused(UIElement element)
{
return (bool)element.GetValue(IsFocusedProperty);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused",
typeof(bool),
typeof(FocusBehavior),
new PropertyMetadata(false, (d, e) =>
{
if ((bool)e.NewValue)
{
var uiElement = d as UIElement;
if (uiElement != null)
{
uiElement.Focus(FocusState.Pointer);
}
}
})
);
}
【问题讨论】:
-
这段除了将焦点放在一个元素上之外什么都不做的代码与“MVVM 中焦点的良好实践”之间有什么关系?你应该澄清一下。 stackoverflow.com/help/how-to-ask