2016-04-25 76 views
0

我有一个WPF的用户控件,其中包含控件,我想把焦点放在一个文本框我试图使用“FocusManager.FocusedElement”,但我不能键入bacause选项卡设置为usercontrol中的第一个控件。如何将光标焦点放在文本框上?

enter image description here

<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="10"/> 
      <RowDefinition Height="45"/> 
      <RowDefinition Height="160"/> 
      <RowDefinition Height="50"/> 
      <RowDefinition Height="15"/> 
      <RowDefinition Height="272*"/> 
     </Grid.RowDefinitions> 

     <Button Grid.Row="1" x:Name="btnCreationOffre" Background="Transparent" Width ="120" Height="35" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="btnCreationOffre_Click" Margin="0,2,0,8" Style="{DynamicResource nv_offreButton}"/> 
     <Button Grid.Row="1" x:Name="btnModifierOffre" Background="Transparent" Width ="120" Height="35" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="btnModifierOffre_Click" Margin="130,2,0,8" Style="{DynamicResource modifierButton}" /> 
     <Button Grid.Row="1" x:Name="btnConsulterOffre" Width ="120" Height="35" Content="Consulter" HorizontalAlignment="left" Click="btnConsulterOffre_Click" Margin="260,2,0,8" Style="{DynamicResource consulterButton}" /> 
     <Button Grid.Row="1" x:Name="btnSuppOffre" Background="Transparent" Width ="120" Height="35" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="btnSuppOffre_Click" Margin="390,2,0,8" Style="{DynamicResource suppButton}" /> 
     <Rectangle HorizontalAlignment="Stretch" Fill="Gray" Height="2" Grid.Row="1" VerticalAlignment="Bottom" Grid.ColumnSpan="4" /> 

     <Grid x:Name="gridFocus" Grid.Row="2" Grid.ColumnSpan="4"> 

      <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center">Numéro offre</TextBlock> 
      <TextBox x:Name="textBoxNumOffre" Height="30" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Center" /> 

我要的是,当我打开该用户控件,我可以在textboxshown输入上面名为“textBoxNumOffre”

回答

0

尝试增加给你的方法(在你的xaml.cs),其在用户控件加载时调用:

public YourUserControl() 
{ 
    InitializeComponent(); 
    this.Dispatcher.BeginInvoke((Action)delegate 
    { 
     Keyboard.Focus(textBoxNumOffre); 
    }, DispatcherPriority.Render); 
} 
相关问题