嵌入列表框初始焦点的文本框
我想将焦点设置到第一个列表框项目(文本框)。我希望能够立即写入内容,而无需单击它或按任何键。我尝试这个但不起作用:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很愚蠢,但只有你等一下,尝试这个版本才有效:
我正在本地测试,直到我发现这个问题和 fuzquat 答案才修复它,所以在这里给我投票,在那里给他投票:D
无法将焦点设置到 UserControl 的子级
it's stupid but it works only if you wait a moment, try this version:
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
如果其他机构有这个问题。 UIElement 必须完全加载才能聚焦。因此这可以变得非常简单:
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: