本文介绍了请告诉我Keyboard.Focus(项目)和item.Focus()之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,有两种方法可将焦点设置给一个元素。
您可以拨打输入元素的.Focus()方法,或致电Keyboard.Focus()的input元素作为参数。

In WPF, there are two ways to set the focus to an element.
You can either call the .Focus() method of the input element, or call Keyboard.Focus() with the input element as parameter.

// first way:
item.Focus();
// alternate way:
Keyboard.Focus(item);

这两者之间的区别是什么?是否有特殊原因在某些情况下使用其他的其中之一呢?
到目前为止,我发现没有什么区别 - 什么都的方法我用过,该项目一直得到了逻辑焦点以及键盘焦点

What is the difference between these two? Are there special reasons to use one of them instead of the other in some cases?
So far I noticed no difference - what ever method I used, the item always got logical focus as well as keyboard focus.

推荐答案

其中的第一件事情是 item.Focus()所做的是调用 Keyboard.Focus(本)。如果失败,那么它会调用的FocusManager ,因为decasteljau已经回答了。

One of the first things that item.Focus() does is call Keyboard.Focus( this ). If that fails, then it makes calls to FocusManager, as decasteljau has answered.

下面从disassambler视图反射。

The following are copied from disassambler view in Reflector.

这是从的UIElement UIElement3D 是一样的):

public bool Focus()
{
    if (Keyboard.Focus(this) == this)
    {
        return true;
    }
    if (this.Focusable && this.IsEnabled)
    {
        DependencyObject focusScope = FocusManager.GetFocusScope(this);
        if (FocusManager.GetFocusedElement(focusScope) == null)
        {
            FocusManager.SetFocusedElement(focusScope, this);
        }
    }
    return false;
}

这是从 ContentElement的

public bool Focus()
{
    return (Keyboard.Focus(this) == this);
}

这篇关于请告诉我Keyboard.Focus(项目)和item.Focus()之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:43