求救
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; }
private void button1_Click(object sender, RoutedEventArgs e) { TextBox txtbox = new TextBox() { Width = 250, Height = 23 }; txtbox.Text = expanders.Header.ToString(); txtbox.Loaded += new RoutedEventHandler(txtbox_Loaded); expanders.Header = txtbox; } void txtbox_Loaded(object sender, RoutedEventArgs e) { TextBox txtbox = (TextBox)sender; txtbox.Focus(); }
正解
txtbox.Focus();写在最后就可以了。
你 這個 TextBox 本沒有放到窗體上哦。
设置button 的Focusable="False"试试
<StackPanel >
<Expander Name="expanders" Header="我要选择中你,然后直接在键盘输入字">
<WrapPanel Orientation="Vertical">
<Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" Focusable="False"/>
</WrapPanel>
</Expander>
</StackPanel>