当 UserControl 更改可见性时聚焦于 TextBox

发布于 2024-09-07 08:39:44 字数 489 浏览 3 评论 0原文

我在画布内加载了一个用户控件;默认情况下,此用户控件的可见性已折叠。当我的窗口的特定文本框聚焦时,用户控件变得可见。

当用户控件变得可见时,我想将焦点设置到用户控件内的另一个文本框。

我尝试这样做:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

≈。彩虹 2024-09-14 08:39:44

好吧,我以这种方式解决:

private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    if (this.Visibility == Visibility.Visible)
    {
        this.Dispatcher.BeginInvoke((Action)delegate
        {
            Keyboard.Focus(TextBlockInput);
        }, DispatcherPriority.Render);
    }
}

我认为问题在于焦点调用 IsVisibleChanged 事件“范围”......对吗?

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?

扮仙女 2024-09-14 08:39:44

请尝试

Keyboard.Focus(TextBlockInput);

参阅此处了解更多详细信息

try

Keyboard.Focus(TextBlockInput);

see here for more details

掩于岁月 2024-09-14 08:39:44

另一种可能的解决方法是使用 Opacity 代替 Visibility 属性。在这种情况下,调用 Focus() 实际上设置了焦点。

Another possible workaround is instead of Visibility property use Opacity. In this case calling Focus() actually sets focus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文