自定义WPF PasswordBox不会收到鼠标单击事件

时间:2012-10-04 14:30:04

标签: c# wpf

我的WPF表单有一个自定义的PasswordBox,这个PasswordBox似乎没有收到鼠标点击事件。当我点击它没有Caret出现。这是我对PasswordBox的风格:

    <Style x:Key="password" TargetType="{x:Type PasswordBox}">
        <Setter Property="Controller:PasswordBoxMonitor.IsMonitoring" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type PasswordBox}">
                    <Border Name="Bd" Background="{TemplateBinding Background}"
                BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"
                SnapsToDevicePixels="true">
                        <Grid>
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            <my:QETextBlock Text="Mật khẩu" Margin="0, 0, 0, 0"  Visibility="Collapsed" FontStyle="Normal" Name="txtPrompt" FontSize="12" FontFamily="Segoe UI"  />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                        </Trigger>
                        <Trigger Property="Controller:PasswordBoxMonitor.PasswordLength" Value="0">
                            <Setter Property="Visibility" TargetName="txtPrompt" Value="Visible" />
                        </Trigger>

                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Visibility" TargetName="txtPrompt" Value="Hidden" />
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我尝试通过为此控件的鼠标按下事件添加事件处理程序来使此控件成为焦点:

    txtPassword.MouseDown += txtPassword_MouseDown;

我的txtPassword_MouseDown函数:

    private void txtPassword_MouseDown(object sender, MouseButtonEventArgs e)
    {
        txtPassword.Focus();
    }

但是当我点击PasswordBox时,这个功能不会被执行。

1 个答案:

答案 0 :(得分:0)

我解决了。有人设置了IsHitTestVisible="False",这就是为什么它没有响应鼠标单击的原因。