如何在自动填充文本框中设置焦点

时间:2015-04-21 07:42:25

标签: wpf

我已下载第三方自动填充文本框并在我的项目中引用它。我有3个自动完成文本框FirstName,LastName,ReceiptNo。当表单加载时,我希望firstname成为焦点。如何实现这一点。

我尝试过几个步骤,比如

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

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

我还尝试创建一个bool isvisible属性并将其绑定到Xaml中的自动完成FirstName文本框,但这不起作用。任何帮助将不胜感激。

我的xaml代码如下所示

<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 个答案:

答案 0 :(得分:1)

你的第一次尝试非常接近。尝试在Page_Loaded_1事件处理程序

中执行以下操作

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

(注意 - MoveFocus是Window类的一个方法,它不是你需要实现的东西)

您应该将文本框定义为具有制表符索引。页面加载后,TraversalRequest将焦点放在第一个制表符索引控件上。如果没有定义选项卡索引,我相信它会将焦点放在UI层次结构的顶部,因此从技术上讲,主窗口将在这种情况下获得焦点。

供参考,此处FocusNavigationDirection选项MSDN link

答案 1 :(得分:0)

我更改了如下所示的代码并且工作正常。我添加了一个cavas作为容器,并将我的自动完整文本框放在画布中。

    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);
        }