我想将焦点设置在应用程序中的控件上,而不将焦点放在整个应用程序上。
例如:单击一个需要一段时间才能加载屏幕的按钮,当加载屏幕时,将焦点设置在其中一个控件上。与此同时,我去另一个应用程序做某事,焦点又回到了前一个应用程序。
当我使用键盘焦点或逻辑焦点时会发生这种情况。
有没有办法阻止这种情况发生?
这是代码:
private void SetFocusInternal()
{
// Focus the first control we find with the 'PositionCursor' indicator expression
FrameworkElement controlToFocus = GetFirstRequiresFocusControl();
// Give focus back to the control which last had it (if any)
if (controlToFocus == null)
controlToFocus = GetLastFocusedControl();
// Just focus the first thing we can find
if (controlToFocus == null)
controlToFocus = GetFirstFocusableControl();
// Using any of the following goes wrong!!
controlToFocus.Focus();
Keyboard.Focus(controlToFocus);
FocusManager.SetFocusedElement(FocusManager.GetFocusScope(this), controlToFocus);
}