3

我有一个带有文本框的 WPF UserControl。以下是文本框及其父控件的定义方式:

   <DockPanel Margin="10,20,10,10" FocusManager.FocusedElement="{Binding ElementName=uxJobNumber}" >
     <TextBox x:Name="uxJobNumber" Text="{Binding JobNumber, Mode=TwoWay, ValidatesOnDataErrors=True}" TextWrapping="Wrap" FontSize="48" BorderBrush="Black" BorderThickness="1"  Margin="10"/>
   </DockPanel>

设置 FocusManager.FocusedElement 后,我​​可以在文本框中看到一个光标栏。但是,光标栏不闪烁,并且不允许用户立即开始输入。

如果没有设置 FocusManager.FocusedElement,当应用程序启动时,文本框中根本没有光标栏。

这是完整的 XAML

<UserControl x:Class=""
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:converters="clr-namespace:.Modules.Converters"
         xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
         xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
         mc:Ignorable="d">
<UserControl.Resources>
    <converters:VisibilityConverter x:Key="Visibility" />
</UserControl.Resources>
<Canvas Width="1024" Height="768" >

    <Border Style="{DynamicResource GroupBox}" Canvas.Left="36.261" Canvas.Top="32.131" Width="426.936">
        <StackPanel>
            <Border>
                <TextBlock Text="STEP 1"/>
            </Border>
            <TextBlock Text="Enter the five (5) digit Job Number and click Verify." />
            <Path/>
            <DockPanel Margin="10,20,10,10" FocusManager.FocusedElement="{Binding ElementName=uxJobNumber}" >
                <Button Content="Verify" Width="125" Height="65" HorizontalAlignment="Right" Command="{Binding SearchJobCommand}" Style="{DynamicResource RedButton}" Margin="0" DockPanel.Dock="Right" IsDefault="True"/>
                <TextBox Text="{Binding JobNumber, Mode=TwoWay, ValidatesOnDataErrors=True}" TextWrapping="Wrap" FontSize="48" BorderBrush="Black" BorderThickness="1" x:Name="uxJobNumber" Margin="10" KeyboardNavigation.TabIndex="0" />
            </DockPanel>
        </StackPanel>
    </Border>
    <TextBlock Text="{Binding Error}" Visibility="{Binding HasError, Converter={StaticResource Visibility}}" Canvas.Left="48" Canvas.Top="288" FontSize="16" Width="403" Foreground="Red" />       
</Canvas>

4

2 回答 2

2

当表单完成加载时,我们最终在后面的代码中使用了 Focus() 方法。

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
   uxJobNumber.Focus();
}
于 2011-06-22T17:42:13.743 回答
0

使用您的代码,我可以正常工作;眨眼和所有。

于 2011-06-17T17:54:47.197 回答