3

I have a WPF UserControl with a textbox. Here's how the textbox and it's parent control are defined:

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

With the FocusManager.FocusedElement set, I can see a cursor bar present within the textbox. However, the cursor bar is not blinking, and does not allow the user to immediately start typing.

Without the FocusManager.FocusedElement set, when the application starts there is no cursor bar within the text box at all.

Here's the complete 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>

Nick Heidke
  • 2,787
  • 2
  • 34
  • 58

2 Answers2

2

We finally resorted to using the Focus() method in the code behind when the form is done loading.

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
   uxJobNumber.Focus();
}
Nick Heidke
  • 2,787
  • 2
  • 34
  • 58
  • Thanks. I had the same problem and reading your answer decided no to lose any time about debugging this. Not being a MVVM orthodox believer your solution is perfectly fine with me. – Dabblernl Jul 06 '13 at 08:42
  • I too was having the same problem, and resorting to code behind worked. I am using mahapps in my application and the textbox is inside a TransitioningContentPresenter; maybe that is affecting it. – Zach Green Mar 27 '14 at 14:35
0

Using your code I get this to work; blinking and all.

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
  • I edited my question to include the entire XAML document with some hope that others may be able to replicate the issue. Thanks for looking into this. – Nick Heidke Jun 18 '11 at 19:28