1

我的 WPF 表单有一个自定义的 PasswordBox,这个 PasswordBox 似乎没有收到鼠标点击事件。当我单击它时,不会出现插入符号。这是我对 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 时,这个功能不会被执行。

4

1 回答 1

1

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

于 2018-09-24T14:57:30.980 回答