アプリケーション全体にフォーカスを与えるのではなく、アプリケーション内のコントロールにフォーカスを設定したいと考えています。
例: 画面の読み込みに時間がかかるボタンをクリックし、画面が読み込まれたら、コントロールの 1 つにフォーカスを設定します。その間、何かをするために別のアプリケーションに移動し、フォーカスが前のアプリケーションに戻りました。
これは、キーボード フォーカスまたは論理フォーカスを使用すると発生します。
これを止める方法はありますか?
コードは次のとおりです。
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);
}