首页 > 其他分享 >WPF 密码提示框

WPF 密码提示框

时间:2023-08-08 15:22:54浏览次数:39  
标签:passwordBox 密码 bool typeof static WPF 提示框 public dp

首先展示效果

 

如图密码提示框,可通过小眼睛进行显示和隐藏密码

1、自定义控件

public class TitlePasswordBox : TextBox
{
/// <summary>
/// 密码提示框提示语
/// </summary>
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}

public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(TitlePasswordBox), new PropertyMetadata(""));


/// <summary>
/// 是否显示密码
/// </summary>
public bool IsDisplay
{
get { return (bool)GetValue(IsDisplayProperty); }
set { SetValue(IsDisplayProperty, value); }
}

public static readonly DependencyProperty IsDisplayProperty =
DependencyProperty.Register("IsDisplay", typeof(bool), typeof(TitlePasswordBox), new PropertyMetadata(false));

}

2、控件模板

<Style TargetType="controls:TitlePasswordBox">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Foreground" Value="#30FFFFFF"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="SelectionBrush" Value="#DDDDDD"></Setter>
<Setter Property="CaretBrush" Value="White"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:TitlePasswordBox">
<Border x:Name="border" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="#666666" SnapsToDevicePixels="True" Background="{Binding Background}">
<Grid>
<TextBlock Text="{TemplateBinding Title}" Foreground="#858484" IsHitTestVisible="False" VerticalAlignment="Center" FontSize="{Binding FontSize}" x:Name="Title" Visibility="Hidden"/>
<PasswordBox Helper:PasswordBoxHelper.Attach="True"
Helper:PasswordBoxHelper.Password="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Foreground="#FFFFFF" CaretBrush="White" BorderThickness="0" Background="Transparent" HorizontalContentAlignment="Left" FontSize="16" PasswordChar="*" Margin="-5,5,0,0" Visibility="{TemplateBinding IsDisplay,Converter={StaticResource BoolToVisInverseConverter}}"/>
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Visibility="{TemplateBinding IsDisplay,Converter={StaticResource BoolToVisConverter}}"/>

</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="#888888"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="#AAAAAA"/>
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" TargetName="Title" Value="Visible" />
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" TargetName="Title" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="SelectionBrush" Value="#AAAAAA"/>
</MultiTrigger>
</Style.Triggers>
</Style>

 

3、界面按钮样式

<Border  BorderBrush="#30FFFFFF" BorderThickness="1" >
<StackPanel Orientation="Horizontal">
<controls:TitlePasswordBox Margin="5,0,0,0" Title="请输入密码" HorizontalAlignment="Left" IsDisplay="{Binding ElementName=IsShow,Path=IsChecked}" x:Name="Password" Text="{Binding Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="245" VerticalAlignment="Center" BorderThickness="0" Background="Transparent" Foreground="White" VerticalContentAlignment="Center"/>
<CheckBox x:Name="IsShow" IsChecked="False" Grid.Column="2" HorizontalAlignment="Left" Margin="0,0,0,0" Width="19.5" Height="15">
<CheckBox.Style>
<Style TargetType="CheckBox">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">

//图片为小眼睛
<Image Source="/Resource/Images/GameField/ConfigurationList/IDHidden.png" Stretch="UniformToFill" x:Name="img"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Source" TargetName="img" Value="/Resource/Images/GameField/ConfigurationList/IDShow.png"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</CheckBox.Style>
</CheckBox>
</StackPanel>
</Border>

 

4、代码中使用PasswordBoxHelper,代码如下

public static class PasswordBoxHelper
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password",
typeof(string), typeof(PasswordBoxHelper),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty AttachProperty =
DependencyProperty.RegisterAttached("Attach",
typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach));
private static readonly DependencyProperty IsUpdatingProperty =
DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
typeof(PasswordBoxHelper));


public static void SetAttach(DependencyObject dp, bool value)
{
dp.SetValue(AttachProperty, value);
}
public static bool GetAttach(DependencyObject dp)
{
return (bool)dp.GetValue(AttachProperty);
}
public static string GetPassword(DependencyObject dp)
{
return (string)dp.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject dp, string value)
{
dp.SetValue(PasswordProperty, value);
}
private static bool GetIsUpdating(DependencyObject dp)
{
return (bool)dp.GetValue(IsUpdatingProperty);
}
private static void SetIsUpdating(DependencyObject dp, bool value)
{
dp.SetValue(IsUpdatingProperty, value);
}
private static void OnPasswordPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
passwordBox.PasswordChanged -= PasswordChanged;
if (!(bool)GetIsUpdating(passwordBox))
{
passwordBox.Password = (string)e.NewValue;
}
passwordBox.PasswordChanged += PasswordChanged;
}
private static void Attach(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
if (passwordBox == null)
return;
if ((bool)e.OldValue)
{
passwordBox.PasswordChanged -= PasswordChanged;
}
if ((bool)e.NewValue)
{
passwordBox.PasswordChanged += PasswordChanged;
}
}
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
SetIsUpdating(passwordBox, true);
SetPassword(passwordBox, passwordBox.Password);
SetIsUpdating(passwordBox, false);
}
}

PasswordBoxHelper编写,转载来源为:https://blog.csdn.net/cxb2011/article/details/89642096

 

以上代码就此完结,如有雷同请联系我

标签:passwordBox,密码,bool,typeof,static,WPF,提示框,public,dp
From: https://www.cnblogs.com/FlyingPigs/p/17614442.html

相关文章

  • WPF c# 使用Emit自动创建通知类
    参考概念参考自https://www.codewrecks.com/post/old/2008/08/implement-inotifypropertychanged-with-dynamic-code-generation/和DevExpress的MVVM框架Runtime-generatedPOCOViewModels代码实现来自ChatGPT抽卡原始类publicclassClass1{publicvirtualint......
  • js记住用户名密码
    现在很多浏览器都会提供是否记住密码的功能。当我们写登录模块是 如果是使用form提交则能被浏览器识别到但是form提交会在链接上暴露出传的参数如果是用js做的登录 浏览器是检测不到的这样我们需要自己加上js记住密码的功能 主要用到了cookie.js以及login.jsp代码如下:co......
  • 037密码生成工具
    一、pwgen[OPTION][pw_length][num_pw](1)OPTION-c选项指定生成的密码包含大小写字母,-c,--capitalizeIncludeatleastonecapitalletterinthepassword.Thisisthedefaultifthestandardoutputisattydevice.-n选项指定生成的密码......
  • 通过vcenter提取主机配置文件方式修改esxi root密码
    1,登陆vcenter2,右击任意一台主机-->主机配置文件-->提取主机配置文件3,为新的主机配置命名,如change-passwd4,选择vcenter管理菜单-->策略与配置文件,修改主机配置文件5,编辑主机配置文件-->编辑-->搜索root-->去掉其它选项-->在密码类型中选择固定的密码配置-->保存(这一步要一定小心,不......
  • 你们眼睛干涩,胀痛吗?C# WPF 久坐提醒桌面小程序
    目录说明设置提醒时间,及休息时间久坐提醒倒计时休息提醒倒计时休息到计时代码说明主窗体设置工作到计时休息倒计时源码久坐提醒桌面小程序依赖库.NetFramework4.5HandyControl3.2网上下的一些小程序,达不到自己想要的效果,要么就是功能复杂,于是选择了自己开发。采用WPF......
  • mysql密码破解
    mysql破解密码1.设置mysql免密登陆#vim/etc/my.cnf.d/mysql-server.cnf#主配置文件[mysqld]下面添加以下内容[mysqld]skip-grant-tables#免密的登陆#重启mysql#systemctlrestartmysqld2.查看数据库中用户的密码,此时是加密状态mysql>selecthost,user,au......
  • 使用 Vim 给文件设置密码保护
    Vim 是一种流行的、功能丰富的和高度可扩展的 Linux文本编辑器,它的一个特殊功能便是支持用带密码各种的加密方法来加密文本文件。本文中,我们将向你介绍一种简单的Vim使用技巧:在 Linux 中使用Vim对文件进行密码保护。我们将向你展示如何让一个文件在它创建的时侯以及为了修......
  • 小米手机锁屏设置了密码,设置了指纹,但是锁屏无效,直接进入桌面,不需要输入密码或指纹
    问题:小米手机锁屏无效?小米手机锁屏设置了密码,设置了指纹,但是锁屏无效,直接进入桌面,不需要输入密码或指纹?小米手机防误触怎么设置?起因:手机放在口袋里,经常会误触。导致手机自动解锁,然后打开各种应用,比如:微信,和拼多多,然后莫名其妙自动下单,导致经常买了一些不需要的东西,东西收到......
  • Linux下轻松修改MySQL/MariaDB的Root密码
    如果你是第一次安装MySQL或MariaDB,你可以执行mysql_secure_installation 脚本来实现基本的安全设置。其中的一个设置是数据库的root密码——该密码必须保密,并且只在必要的时候使用。如果你需要修改它(例如,当数据库管理员换了人——或者被解雇了!)。修改MySQL或......
  • c#串口通信讲解(一)(winform、wpf)
    转载:https://blog.csdn.net/weixin_30466421/article/details/99278174串口操作需要注意的几点如下:1、如果是USB转串口;则需要安装USB转串口驱动,附件有此驱动。2、串口打开状态最好不要直接插拔串口,可能会导致中控板或者串口线烧坏。3、使用串口调试工具CEIWEI,下一章节会贴上......