這是愚蠢的,但它只能如果你稍等一下,試試這個版本:
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
Can't set focus to a child of UserControl
你現在是否得到'NullReferenceException'? –
@BalaR nop。我不能直接寫,我需要點擊控制或按Tab鍵。 – mjsr
嘗試顯示事件而不是加載? –