从这篇帖子WPF:如何以编程方式从TextBox中移除焦点,我知道如何使用以下代码将TextBox
的焦点设置回其父级:
// Move to a parent that can take focus
FrameworkElement parent = (FrameworkElement)textBox.Parent;
while (parent != null && parent is IInputElement
&& !((IInputElement)parent).Focusable)
{
parent = (FrameworkElement)parent.Parent;
}
DependencyObject scope = FocusManager.GetFocusScope(textBox);
FocusManager.SetFocusedElement(scope, parent as IInputElement);
有没有一种方法可以将这个代码(如模板函数)泛化,使其也适用于其他项目,如ComboBox
、Canvas
、Image
等?