繁体   English   中英

如何将光标放在文本框上?

[英]how to put cursor focus on textbox?

我有一个包含控件的WPF用户控件,我想将重点放在我尝试使用“ FocusManager.FocusedElement”的文本框上,但是我无法键入,因为选项卡设置为usercontrol中的第一个控件。

在此处输入图片说明

<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" />

我想要的是打开此用户控件时可以在上面显示的名为“ textBoxNumOffre”的文本框中键入

尝试将其添加到您的方法(在您的xaml.cs中)中,该方法在加载usercontrol时被调用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM