textBox中嵌入在ListBox最初的焦点 [英] textBox embedded in a ListBox initial focus

查看:224
本文介绍了textBox中嵌入在ListBox最初的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将焦点设置到这是一个文本框的第一个列表框项目。我希望能够在这immedatelly写不necesity点击它或按任意键。我尝试这一点,但不工作:

 私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
listBox1.Items.Add(新文本框(){});
(listBox1.Items [0]作为文本框)。重点();

}


解决方案

这是愚蠢的但它只能如果你稍等一下,试试这个版本:

 使用系统;使用System.Windows 
;使用System.Windows.Controls的
;

命名空间WpfApplication1
{
公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent() ;
}

私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
变种的textBox =新的TextBox(){};
listBox1.Items.Add(的textBox);

System.Threading.ThreadPool.QueueUserWorkItem(
(A)=>
{
System.Threading.Thread.Sleep(100);
textBox.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
新的行动(
委托()
{
textBox.Focus();
}
));
}
);
}
}
}



我在本地测试,并可能不解决这个问题,直到我发现在那里这个问题,答案fuzquat投票等等我在这里和他在那里:D



WPF - 无法将焦点设置到用户控件


的孩子

I want to set the focus to the first ListBox item that is a textbox. I want to be able to write in it immedatelly without the necesity to click it or press any key. I try this but doesn't work:

        private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        listBox1.Items.Add(new TextBox() { });
        (listBox1.Items[0] as TextBox).Focus();

    }

解决方案

it's stupid but it works only if you wait a moment, try this version:

using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var textBox = new TextBox() {};
            listBox1.Items.Add(textBox);

            System.Threading.ThreadPool.QueueUserWorkItem(
                (a) =>
                {
                    System.Threading.Thread.Sleep(100);
                    textBox.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                            delegate()
                            {
                                textBox.Focus();
                            }
                            ));
                }
                );
        }
    }
}

I was testing locally and could not fix it until I found this question and fuzquat answer in there so vote me here and him there :D

WPF - Can't set focus to a child of UserControl

这篇关于textBox中嵌入在ListBox最初的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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