无法将焦点设置到 UserControl 的子级 [英] Can't set focus to a child of UserControl

查看:17
本文介绍了无法将焦点设置到 UserControl 的子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl,其中包含一个 TextBox.当我的主窗口加载时,我想将焦点设置到这个文本框,所以我将 Focusable="True" GotFocus="UC_GotFocus" 添加到 UserControl 的定义和 FocusManager.FocusedElement="{Binding ElementName=login}" 到我的主窗口定义.在 UC_GotFocus 方法中,我只需在要关注的控件上调用 .Focus() ,但这不起作用.

I have a UserControl which contains a TextBox. When my main window loads I want to set the focus to this textbox so I added Focusable="True" GotFocus="UC_GotFocus" to the UserControls definition and FocusManager.FocusedElement="{Binding ElementName=login}" to my main windows definition. In the UC_GotFocus method i simply call .Focus() on the control i want to focus on but this doesn't work.

我需要做的就是在 UserControl 中有一个 TextBox 在应用程序启动时接收焦点.

All i need to do is have a TextBox in a UserControl receive focus when the application starts.

任何帮助将不胜感激,谢谢.

Any help would be appreciated, thanks.

推荐答案

我最近为第一次加载主窗口时通过情节提要显示的登录启动画面修复了这个问题.

I recently fixed this problem for a login splash screen that is being displayed via a storyboard when the main window is first loaded.

我相信修复的关键有两个.一个是使包含元素成为焦点范围.另一个是处理由正在加载的窗口触发的情节提要的情节提要完成事件.

I believe there were two keys to the fix. One was to make the containing element a focus scope. The other was to handle the Storyboard Completed event for the storyboard that was triggered by the window being loaded.

此故事板使用户名和密码画布可见,然后淡入为 100% 不透明.关键是用户名控件在情节提要运行之前不可见,因此该控件在可见之前无法获得 keyboard 焦点.让我失望的是它有焦点"(即焦点是真实的,但事实证明这只是逻辑焦点),直到阅读 Kent Boogaart 的文章,我才知道 WPF 有逻辑焦点和键盘焦点的概念回答并查看微软的 WPF 链接文本

This storyboard makes the username and password canvas visible and then fades into being 100% opaque. The key is that the username control was not visible until the storyboard ran and therefore that control could not get keyboard focus until it was visible. What threw me off for awhile was that it had "focus" (i.e. focus was true, but as it turns out this was only logical focus) and I did not know that WPF had the concept of both logical and keyboard focus until reading Kent Boogaart's answer and looking at Microsoft's WPF link text

一旦我这样做了,我的特定问题的解决方案就很简单了:

Once I did that the solution for my particular problem was straightforward:

1) 使包含元素成为焦点范围

1) Make the containing element a focus scope

<Canvas FocusManager.IsFocusScope="True" Visibility="Collapsed">
    <TextBox x:Name="m_uxUsername" AcceptsTab="False" AcceptsReturn="False">
    </TextBox>
</Canvas>

2) 将已完成的事件处理程序附加到情节提要

2) Attach a Completed Event Handler to the Storyboard

    <Storyboard x:Key="Splash Screen" Completed="UserNamePassword_Storyboard_Completed">
...
</Storyboard>

3) 将我的用户名 TextBox 设置为在情节提要完成事件处理程序中获得键盘焦点.

3) Set my username TextBox to have the keyboard focus in the storyboard completed event handler.

void UserNamePassword_Storyboard_Completed(object sender, EventArgs e)
{
 m_uxUsername.Focus();
}

请注意,调用 item.Focus() 会导致调用 Keyboard.Focus(this),因此您无需显式调用 this.请参阅有关 Keyboard.Focus(item) 和项目.焦点.

Note that calling item.Focus() results in the call Keyboard.Focus(this), so you don't need to call this explicitly. See this question about the difference between Keyboard.Focus(item) and item.Focus.

这篇关于无法将焦点设置到 UserControl 的子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆