1
votes

I have downloaded 3rd party autocomplete textbox and referenced it in my project. I have 3 autocomplete textboxes FirstName, LastName, ReceiptNo. When the form loads I want the firstname to be in focus. How to achieve this.

I have tried several steps like

//Eventhandler while form loads
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
    FirstName.Focus();
}

or

//Eventhandler while autocomplete textbox loads  
void FirstName_GotFocus(object sender, RoutedEventArgs e)
{
    FirstName.Focus();
}

I also tried creating a bool isvisible property and binding it to autocomplete FirstName textbox in Xaml but that does not works. Any help will be appreciated.

My xaml code is given below

<wpf:AutoCompleteTextBox  Style="{StaticResource AutoComp}" 
                          Height="32"
                          Canvas.Left="33"
                          ToolTip="First Name"
                          Canvas.Top="120"
                          Width="205"
                          Padding="10,5"
                          TabIndex="1001"
                          VerticalAlignment="Top"
                          Loaded="FirstName_GotFocus"                      
                          Watermark=""
                          IconPlacement="Left"
                          IconVisibility="Visible"
                          Delay="100"
                          Text="{Binding FirstName, Mode=TwoWay, TargetNullValue=''}" 
                          Provider="{Binding FirstNameSuggestions}">
    <wpf:AutoCompleteTextBox.ItemTemplate>
        <DataTemplate>
            <Border Padding="5">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding}"
                               FontWeight="Bold" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </wpf:AutoCompleteTextBox.ItemTemplate>
</wpf:AutoCompleteTextBox>

<Label Style="{StaticResource Devlbl}"
       Canvas.Left="250"
       Content="Last Name"
       Canvas.Top="90" />
    <wpf:AutoCompleteTextBox Style="{StaticResource AutoComp}"
                             Height="32"
                             ToolTip="Last Name"
                             Canvas.Left="250"
                             Canvas.Top="120"
                             Width="205"
                             Padding="10,5"
                             TabIndex="1002"
                             VerticalAlignment="Top"
                             Watermark=""
                             IconPlacement="Left"
                             IconVisibility="Visible"
                             Delay="100"
                             Text="{Binding LastName, Mode=TwoWay, TargetNullValue=''}" 
                             Provider="{Binding LastNameSuggestions}">
        <wpf:AutoCompleteTextBox.ItemTemplate>
            <DataTemplate>
                <Border Padding="5">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding}"
                                   FontWeight="Bold" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </wpf:AutoCompleteTextBox.ItemTemplate>
    </wpf:AutoCompleteTextBox>
</Label>
<Label Style="{StaticResource Devlbl}"
       Canvas.Left="480"
       Content="Receipt No"
       Canvas.Top="90" />
    <!--<TextBox Canvas.Left="480"
             ToolTip="Receipt No"
             Canvas.Top="107"
             Width="205"
             MaxLength="10"
             TabIndex="1003"
             Style="{StaticResource CommonTextBox}"      
             Text="{Binding ReceiptNo,TargetNullValue=''}">
        <i:Interaction.Behaviors>
            <b:AllowableCharactersTextBoxBehavior RegularExpression="^[0-9]+$" MaxLength="10" />
        </i:Interaction.Behaviors>
    </TextBox>-->
    <wpf:AutoCompleteTextBox Style="{StaticResource AutoComp}"
                             Height="32"
                             ToolTip="Receipt No"
                             Canvas.Left="480"
                             Canvas.Top="120"
                             Width="205"
                             Padding="10,5"
                             TabIndex="1002"
                             VerticalAlignment="Top"
                             Watermark=""
                             IconPlacement="Left"
                             IconVisibility="Visible"
                             Delay="100"
                             Text="{Binding ReceiptNo, Mode=TwoWay, TargetNullValue=''}" 
                             e:FocusExtension.IsFocused="{Binding IsFocused, Mode=TwoWay }"   
                             Provider="{Binding ReceiptIdSuggestions}">
        <wpf:AutoCompleteTextBox.ItemTemplate>
            <DataTemplate>
                <Border Padding="5">
                    <StackPanel Orientation="Vertical" >
                        <TextBlock Text="{Binding}"
                                   FontWeight="Bold">
                        </TextBlock>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </wpf:AutoCompleteTextBox.ItemTemplate>
        <i:Interaction.Behaviors>
            <b:AllowableCharactersTextBoxBehavior RegularExpression="^[0-9]+$" MaxLength="15" />
        </i:Interaction.Behaviors>
    </wpf:AutoCompleteTextBox>
</Label>
2

2 Answers

1
votes

Your first attempt is very close. Try doing the following in the Page_Loaded_1 event handler

this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

(Note - MoveFocus is a method on the Window class, it's not something you need to implement)

You should define your textboxes to have tab-indices. After the page loads, the TraversalRequest will give focus to the first tab-indexed control. If no tab indices are defined I believe it will give focus to the top of the UI hierarchy, so technically the main window would receive focus in that case.

For reference, here's an MSDN link to all of the FocusNavigationDirection options.

0
votes

I changed the code as shown below and its working fine. I added a cavas as container and placed my auto complete textbox inside the canvas.

    FocusNavigationDirection focusDirection = FocusNavigationDirection.Next;

        // MoveFocus takes a TraveralReqest as its argument.
        TraversalRequest request = new TraversalRequest(focusDirection);
        UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
        if (elementWithFocus != null)
        {
            elementWithFocus.MoveFocus(request);
        }