2016-04-25 6 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" /> 

То, что я хочу, когда я открыть этот UserControl я могу ввести в textboxshown вышеназванное «textBoxNumOffre»

ответ

0

Попробуйте добавить это в ваш метод (в вашем xaml.cs), что вызывается при загрузке пользовательского контроля:

public YourUserControl() 
{ 
    InitializeComponent(); 
    this.Dispatcher.BeginInvoke((Action)delegate 
    { 
     Keyboard.Focus(textBoxNumOffre); 
    }, DispatcherPriority.Render); 
} 
Смежные вопросы