将焦点放在控件上而不将应用程序放在前面

时间:2013-01-17 16:09:35

标签: c#

我想将焦点设置为应用程序中的控件,而不关注整个应用程序。

例如:单击一个需要一段时间才能加载屏幕的按钮,当加载屏幕时,将焦点设置在其中一个控件上。与此同时,我已经去了另一个应用程序做某事,焦点返回到之前的应用程序。

当我使用键盘焦点或逻辑焦点时会发生这种情况。

有没有办法阻止这种情况发生?

以下是代码:

    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);
    }

0 个答案:

没有答案