textBox1.Focus();
但是焦点仍然不在文本框上,求解!!!!
5 个解决方案
#1
Keyboard.Focus(textBox1)即可
#2
还是不行!!!!
#3
我自己写的个例子,是可行的,你在使用这个方法的时候, 要确保这个控件已经加载完毕。
sample code:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 版本1,通过Background dispatcher
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action)(() => { Keyboard.Focus(TextBox2); }));
// 版本2,控件完全加载完毕后执行焦点设置
//this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Keyboard.Focus(TextBox2);
}
}
#4
用这个行了 谢谢了
#5
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action)(() => { Keyboard.Focus(TextBox2); }));
#1
Keyboard.Focus(textBox1)即可
#2
还是不行!!!!
#3
我自己写的个例子,是可行的,你在使用这个方法的时候, 要确保这个控件已经加载完毕。
sample code:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 版本1,通过Background dispatcher
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action)(() => { Keyboard.Focus(TextBox2); }));
// 版本2,控件完全加载完毕后执行焦点设置
//this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Keyboard.Focus(TextBox2);
}
}
#4
用这个行了 谢谢了
#5
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action)(() => { Keyboard.Focus(TextBox2); }));