当UserControl更改可视性时,将焦点放在TextBox上 [英] Focus on TextBox when UserControl change Visibility
问题描述
当usercontrol变得可见时,我想将焦点设置到usercontrol内的另一个文本框中。 b
我尝试这样做:
pre $ private $ UserControl_IsVisibleChanged(object sender,DependencyPropertyChangedEventArgs e)
if(this.Visibility == Visibility.Visible)
{
FocusManager.SetFocusedElement(this,TextBlockInput);
$ b
看来工作,但有一个问题:文本框似乎集中,但光标到文本框不闪烁,我不能输入字符的输入。
我会说,焦点后的文本框准备好输入。我该怎么办?
好的,我这样解决:
($ this.Visibility == Visibility.Visible)
{$ b $ private $ UserControl_IsVisibleChanged(object sender,DependencyPropertyChangedEventArgs e)
{
(this)(this)(b)this.Dispatcher.BeginInvoke((Action)delegate
{
Keyboard.Focus(TextBlockInput);
},DispatcherPriority.Render);
$ b $ p
$ b我认为问题是焦点调用IsVisibleChanged事件范围...对吗?
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?
Well, I solve in this way:
private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.Visibility == Visibility.Visible)
{
this.Dispatcher.BeginInvoke((Action)delegate
{
Keyboard.Focus(TextBlockInput);
}, DispatcherPriority.Render);
}
}
I think that the problem was tha focus call into IsVisibleChanged event "scope"...right?
这篇关于当UserControl更改可视性时,将焦点放在TextBox上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!