gpt4 book ai didi

wpf - 获取和恢复 WPF 键盘焦点

转载 作者:行者123 更新时间:2023-12-02 03:48:04 26 4
gpt4 key购买 nike

在 WPF 程序中,我想获取当前(键盘)焦点,存储它并稍后重新设置。

为了立即获得当前焦点,我使用:

DependencyObject focusScope = FocusManager.GetFocusScope(d);
_lastFocus = FocusManager.GetFocusedElement(focusScope);

要稍后设置它,我使用:

if (_lastFocus != null)
{
IInputElement setFocus = _lastFocus;
_lastFocus = null;
d.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new ThreadStart(delegate
{
FocusManager.SetFocusedElement(d, setFocus);
}));
}

这一次有效。但如果我再试一次,它就不起作用,直到我重新创建我尝试将焦点设置到的有问题的对话框,即使它做了同样的事情(我跟踪了获取和设置焦点)。相反,主窗口本身获得焦点。

我曾经听说 WPF 中有两种焦点,我是否还需要设置其他内容才能获得一致的结果?

最佳答案

你是对的。 WPF 具有逻辑焦点(您正在使用逻辑焦点)和键盘焦点。您可以在Focus Overview中找到两者的完整详细信息。 MSDN 页面。从该页面:

键盘焦点

Keyboard focus refers to the element that is currently receiving keyboard input. There can be only one element on the whole desktop that has keyboard focus. In WPF, the element that has keyboard focus will have IsKeyboardFocused set to true. The static property FocusedElement on the Keyboard class gets the element that currently has keyboard focus.

In order for an element to obtain keyboard focus, the Focusable and the IsVisible properties on the base elements must be set to true. Some classes, such as the Panel base class, have Focusable set to false by default; therefore, you must set Focusable to true if you want such an element to be able to obtain keyboard focus.

Keyboard focus can be obtained through user interaction with the UI, such as tabbing to an element or clicking the mouse on certain elements. Keyboard focus can also be obtained programmatically by using the Focus method on the Keyboard class. The Focus method attempts to give the specified element keyboard focus. The returned element is the element that has keyboard focus, which might be a different element than requested if either the old or new focus object block the request.

逻辑焦点

Logical focus refers to the FocusManager.FocusedElement in a focus scope. A focus scope is an element that keeps track of the FocusedElement within its scope. When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus. This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope.

There can be multiple elements that have logical focus in an application, but there may only be one element that has logical focus in a particular focus scope.

An element that has keyboard focus has logical focus for the focus scope it belongs to.

<小时/>

回到你的问题,你没有使用的另一种焦点是 Keyboard.Focus 。您可以像这样使用它:

Keyboard.Focus(theButtonThatYouWantToFocus);
<小时/>

另外,请注意 UIElement.Focus() 方法将尝试将逻辑键盘焦点设置到调用它的元素。它将返回 true 如果键盘焦点和逻辑焦点设置为此元素false 如果仅将此元素设置为逻辑焦点

<小时/>

另一种可用于聚焦控件的方法是使用 FocusManager.FocusedElement Attached Property 。大多数人静态地使用它,在这种情况下,这只会在 View 加载时起作用一次:

<Grid FocusManager.FocusedElement="{Binding ElementName=TextBoxToFocus}">
<TextBox Name="TextBoxToFocus" Text="Focus Me" />
<Grid>

但是,可以DataTrigger 中使用它并将其设置为依赖于自定义 bool属性,在此示例中为 IsFocused属性:

<Style x:Key="FocusableTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused}" Value="True">
<Setter Property="FocusManager.FocusedElement"
Value="{Binding RelativeSource={RelativeSource Self}}" />
</DataTrigger>
</Style.Triggers>
</Style>

所以每当我设置 IsFocused View 模型中的属性为 true,任何具有此 Style 的元素应用将获得逻辑焦点。现在清楚了,这个Style是针对TextBox控制,但如果将其更改为Control,它仍然可以工作。例如。

关于wpf - 获取和恢复 WPF 键盘焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19289994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com