嵌入列表框初始焦点的文本框

发布于 2024-12-06 07:25:50 字数 267 浏览 0 评论 0原文

我想将焦点设置到第一个列表框项目(文本框)。我希望能够立即写入内容,而无需单击它或按任何键。我尝试这个但不起作用:

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

    }

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();

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

望她远 2024-12-13 07:25:50

这很愚蠢,但只有你等一下,尝试这个版本才有效:

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();
                            }
                            ));
                }
                );
        }
    }
}

我正在本地测试,直到我发现这个问题和 fuzquat 答案才修复它,所以在这里给我投票,在那里给他投票:D

无法将焦点设置到 UserControl 的子级

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

Can't set focus to a child of UserControl

<逆流佳人身旁 2024-12-13 07:25:50

如果其他机构有这个问题。 UIElement 必须完全加载才能聚焦。因此这可以变得非常简单:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    listBox1.Items.Add(new TextBox() { });
    var txBox = listBox1.Items[0] as TextBox;
    txBox.Loaded += (txbSender, args) => (txbSender as TextBox)?.Focus();
}

In case any body else has this issue. An UIElement has to be fully loaded before you can focus it. therefor this can be made very simple:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    listBox1.Items.Add(new TextBox() { });
    var txBox = listBox1.Items[0] as TextBox;
    txBox.Loaded += (txbSender, args) => (txbSender as TextBox)?.Focus();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文