无法在WPF TextBox中使用鼠标左键设置焦点

时间:2013-02-20 09:31:35

标签: wpf visual-studio-2008 wpf-controls

在加载时将焦点设置在UserName TextBox上,但是如果我单击密码TextBox然后我使用鼠标单击UserName TextBox,则它不会关注UserName。
我正在使用VS 2008。

<WrapPanel Margin="10" >
    <StackPanel Name="stackPanel5"  Margin="0,20,0,0">
        <StackPanel Height="31" Name="stackPanel2" Orientation="Horizontal" Width="217">
            <Label Margin="15,0,0,0" Height="24" Name="labelUserName" Width="64" FontSize="12" >Username</Label>
            <TextBox Margin="2,1,0,0" Height="24" Name="textboxUsername" Width="120" TabIndex="1" MaxLength="25" />
        </StackPanel>
        <StackPanel Orientation="Horizontal" Height="31" Name="stackPanel3" Width="217">
            <Label Margin="20,0,0,0" Height="24" Name="labelPassword" Width="60" FontSize="12"  >Password</Label>
            <PasswordBox Margin="1,1,0,0" Height="24" Name="passwordBoxUserpassword" Width="120" TabIndex="2" MaxLength="16" CommandManager.PreviewExecuted="passwordBoxUserpassword_PreviewExecuted" />
        </StackPanel>
        <StackPanel Orientation="Horizontal" Height="31" Name="stackPanel7" Width="217" Visibility="Collapsed">
            <Label Margin="10,0,0,0" Height="24" Name="lblApplication" Width="70" FontSize="12" Visibility="Collapsed">Application</Label>
            <ComboBox Margin="1,1,0,0" Height="24" Name="ComboBoxProjects" Width="120" ItemsSource="{Binding}" Visibility="Collapsed"/>
        </StackPanel>


        <StackPanel Orientation="Horizontal" Height="31" Name="stackPanel4" Width="230" Visibility="Collapsed">
            <Label Margin="2,0,0,0" Height="24" Name="labelNewPassword" Width="87" FontSize="12">New Password</Label>
            <PasswordBox Margin="-2,1,0,0" Height="24" Name="passwordBoxNewPassword" Width="120"  />
        </StackPanel>


        <StackPanel Orientation="Horizontal"  Name="stkpnlLogin" Width="230">
            <Label Margin="10,0,0,0" Height="30" Name="lblAppn" Width="70" FontSize="12" >Application</Label>
            <RadioButton Name="rdbCPS" Margin="3,11,3,3" IsChecked="False" TabIndex="3"  VerticalContentAlignment="Center" Content="CPS" Checked="rdbCPS_Checked"/>

            <RadioButton Name="rdbCSS" Margin="3,11,3,3" IsChecked="True" TabIndex="4" VerticalContentAlignment="Center" Content="CSS" Checked="rdbCSS_Checked"/>

        </StackPanel>

        <StackPanel Margin="50,10,10,10" Orientation="Vertical"  Name="stackPanel1" Grid.Row="5" Grid.Column="1" Height="50" Width="153">

            <WrapPanel Orientation="Horizontal" >
                <Button Height="24" Name="buttonSubmit" Style="{StaticResource Glassstyle}" Background="SteelBlue" Width="74"  Click="buttonSubmit_Click" FontWeight="Bold" IsDefault="True" TabIndex="5">Submit</Button>
                <Button Height="24" Name="buttonCancel" Style="{StaticResource Glassstyle}" Background="SteelBlue"  Width="74"  Click="buttonCancel_Click" FontWeight="Bold" TabIndex="6">Cancel</Button>
            </WrapPanel>
            <StackPanel Name="stackPanel6" Margin="2">
                <TextBlock Height="19" Name="textBlock1" Width="100">
                    <Hyperlink Name="HyperlinkChangepassword" Click="HyperlinkChangepassword_Click">Change Password</Hyperlink>
                </TextBlock>
            </StackPanel>
        </StackPanel>
        <!--<StackPanel Height="17" Orientation="Horizontal" Name="stackPanel6" Width="91">
                    <TextBlock Height="19" Name="textBlock1" Width="91">
                        <Hyperlink Name="HyperlinkChangepassword" Click="HyperlinkChangepassword_Click">Change Password</Hyperlink></TextBlock>
                </StackPanel>-->
    </StackPanel>
</WrapPanel>

1 个答案:

答案 0 :(得分:0)

你必须穿上Textbox GotFocus()事件,事件就是说textbox.SelectAll()。