无法将焦点设置为UserControl的子项 [英] Can't set focus to a child of UserControl
问题描述
我有一个 我需要做的是在 我最近修正了这个问题,当首次加载主窗口时,通过故事板显示登录启动画面。 我相信修复有两个关键。一个是使包含元素的焦点范围。另一个是处理由被加载的窗口触发的故事板的Storyboard Completed事件。 这个故事板使用户名和密码画布可见,然后淡入100%不透明。关键是用户名控制在故事板运行前是不可见的,因此控件无法获取键盘焦点,直到可见。是什么让我停下来了一段时间,它有专注(即重点是真实的,但事实证明,这只是合乎逻辑的重点),我不知道WPF有逻辑和键盘焦点的概念,直到阅读Kent Boogaart的回答并查看微软的WPF 链接文本 一旦我这样做了,我的特定问题的解决方案很简单: 和 UserControl
,它包含一个 TextBox
。当我的主窗口加载我想把焦点设置到这个文本框,所以我添加 UserControl的
Focusable =TrueGotFocus =UC_GotFocus
/ code>的定义和 FocusManager.FocusedElement ={Binding ElementName = login}
到我的主窗口定义。在 UC_GotFocus
方法中,我简单地调用我想要关注的控件上的 .Focus()
工作。
UserControl 中有一个
TextBox
$ b $ 1)使包含元素为焦点范围
< Canvas FocusManager.IsFocusScope =TrueVisibility =Collapsed>
< TextBox x:Name =m_uxUsernameAcceptsTab =FalseAcceptsReturn =False>
< / TextBox>
< / Canvas>
2)将一个完成的事件处理程序附加到故事板
< Storyboard x:Key =Splash ScreenCompleted =UserNamePassword_Storyboard_Completed>
...
< / Storyboard>
<3>设置我的用户名TextBox在故事板完成的事件处理程序中拥有键盘焦点。
$ b
void UserNamePassword_Storyboard_Completed(object sender,EventArgs e)
{
m_uxUsername.Focus();
$ b请注意,调用item.Focus()会导致调用Keyboard.Focus这个),所以你不需要明确地调用它。看到这个问题有关的Keyboard.Focus(项目)之间的不同和item.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 UserControl
s 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.
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.
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) 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) Attach a Completed Event Handler to the Storyboard
<Storyboard x:Key="Splash Screen" Completed="UserNamePassword_Storyboard_Completed">
...
</Storyboard>
and
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();
}
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屋!