当UserControl更改可视性时,将焦点放在TextBox上 [英] Focus on TextBox when UserControl change Visibility

查看:211
本文介绍了当UserControl更改可视性时,将焦点放在TextBox上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件加载在画布内;这个默认情况下的用户控件的可见性崩溃了。当我的窗口的特定文本框被关注时,usercontrol变得可见。



当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屋!

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