WPF 设置鼠标焦点在TextBox中就那么难吗?
XAML
<StackPanel >
<Expander Name="expanders" Header="我要选择中你,然后直接在键盘输入字">
<WrapPanel Orientation="Vertical">
<Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
</WrapPanel>
</Expander>
</StackPanel>
Button事件后台创建Textbox并且设置设置
SelectAll、Focus
private void button1_Click(object sender, RoutedEventArgs e) { TextBox txtbox = new TextBox() { Width = 250, Height = 23 }; txtbox.Text = expanders.Header.ToString(); //txtbox.SelectAll(); //txtbox.Focus(); expanders.Header = txtbox; }
TextBox txtbox = null; private void button1_Click(object sender, RoutedEventArgs e) { txtbox = new TextBox() { Width = 250, Height = 23 }; txtbox.Text = expanders.Header.ToString(); expanders.Header = txtbox; txtbox.Loaded += new RoutedEventHandler(txtbox_Loaded); } void txtbox_Loaded(object sender, RoutedEventArgs e) { txtbox.SelectAll(); txtbox.Focus(); }