2016-04-25 145 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); 
} 
相關問題