Set focus on a control without bringing the application to the front
<p>I want to set the focus to a control within an application without giving focus to the whole application.</p>

<p>For example: Click a button which takes a while to load a screen, when the screen is loaded set the focus on one of the controls. In the meantime I have gone to a different application to do something and the focus returns to the previous application.</p>

<p>This happens when I use Keyboard focus or Logical focus.</p>

<p>Is there any way to stop this happening?</p>

<p>Here is the code:</p>

<pre><code>    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);
    }
</code></pre>
