我的 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 时,这个功能不会被执行。