当 UserControl 更改可见性时聚焦于 TextBox
我在画布内加载了一个用户控件;默认情况下,此用户控件的可见性已折叠。当我的窗口的特定文本框聚焦时,用户控件变得可见。
当用户控件变得可见时,我想将焦点设置到用户控件内的另一个文本框。
我尝试这样做:
private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.Visibility == Visibility.Visible)
{
FocusManager.SetFocusedElement(this, TextBlockInput);
}
}
看起来可行,但有一个问题:文本框似乎已聚焦,但文本框中的光标不闪烁,并且我无法键入字符进行输入。
我希望焦点之后文本框就可以输入了。我该怎么办?
I have a usercontrol loaded inside a canvas; this usercontrol on default have visibility collapsed. When a specific textbox of my window is focused the usercontrol become visible.
When usercontrol become visible I want set focus to another textbox inside usercontrol.
I try to do that:
private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.Visibility == Visibility.Visible)
{
FocusManager.SetFocusedElement(this, TextBlockInput);
}
}
It seems work but there is a problem: the textbox seems focused but the cursor into textbox don't blink and I can't type chars for input.
I would that after focus the textbox is ready for input. How can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我以这种方式解决:
我认为问题在于焦点调用 IsVisibleChanged 事件“范围”......对吗?
Well, I solve in this way:
I think that the problem was tha focus call into IsVisibleChanged event "scope"...right?
请尝试
参阅此处了解更多详细信息
try
see here for more details
另一种可能的解决方法是使用
Opacity
代替Visibility
属性。在这种情况下,调用Focus()
实际上设置了焦点。Another possible workaround is instead of
Visibility
property useOpacity
. In this case callingFocus()
actually sets focus.