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