WPF 密码框水印与明文切换

WPF开发者QQ群: 340500857 

欢迎转发、分享、点赞、在看,谢谢~。  

效果预览(更多效果请下载源码体验):

一、PasswordWithPlainText.xaml 代码如下

<UserControl x:Class="WpfPasswrod.CustomControls.PasswordWithPlainText"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"xmlns:local="clr-namespace:WpfPasswrod.CustomControls"Name="uc" Foreground="Black"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><UserControl.Resources><ResourceDictionary><VisualBrush x:Key="HelpBrush" TileMode="None" Opacity="0.3" Stretch="None" AlignmentY="Center" AlignmentX="{Binding AlignmentX,ElementName=uc}"ViewportUnits="RelativeToBoundingBox"><VisualBrush.Viewport><Rect X="0.01" Y="0" Width="1" Height="1"></Rect></VisualBrush.Viewport><VisualBrush.Visual><TextBlock VerticalAlignment="Stretch" HorizontalAlignment="Left" TextAlignment="{Binding TextAlignment,ElementName=uc}"Foreground="{Binding Foreground,ElementName=uc}"Text="{Binding Hint,ElementName=uc}"FontSize="{Binding FontSize,ElementName=uc}"/></VisualBrush.Visual></VisualBrush><SolidColorBrush x:Key="ImageToggleButton.Static.Background" Color="#FFDDDDDD"/><SolidColorBrush x:Key="ImageToggleButton.Static.Border" Color="#FF707070"/><SolidColorBrush x:Key="ImageToggleButton.MouseOver.Background" Color="#FFBEE6FD"/><SolidColorBrush x:Key="ImageToggleButton.IsChecked.Background" Color="#FFBEE6FD"/><Style TargetType="{x:Type local:ImageToggleButton}"><Setter Property="SnapsToDevicePixels" Value="true"/><Setter Property="UseLayoutRounding" Value="True"/><Setter Property="VerticalContentAlignment" Value="Center"/><Setter Property="HorizontalContentAlignment" Value="Center"/><Setter Property="Background" Value="{StaticResource ImageToggleButton.Static.Background}"/><Setter Property="BorderBrush" Value="{StaticResource ImageToggleButton.Static.Border}"/><Setter Property="Cursor" Value="Hand"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:ImageToggleButton}"><Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"Background="{TemplateBinding Background}"SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"UseLayoutRounding="True"CornerRadius="{Binding Path=CornerRadius, RelativeSource={RelativeSource Mode=TemplatedParent}}"HorizontalAlignment="Stretch" VerticalAlignment="Stretch"><Grid><Image x:Name="image"  Focusable="False" Margin="0,1,0,0"Source="{Binding NormalImage, RelativeSource={RelativeSource TemplatedParent}}"HorizontalAlignment="Center" VerticalAlignment="Center" Height="20" Width="20"/></Grid></Border><ControlTemplate.Triggers><Trigger Property="IsChecked" Value="true"><Setter TargetName="border" Property="Background" Value="{StaticResource ImageToggleButton.IsChecked.Background}"/><Setter TargetName="image" Property="Source" Value="{Binding UnImage, RelativeSource={RelativeSource TemplatedParent}}"/></Trigger><Trigger Property="IsMouseOver" Value="true"><Setter TargetName="border" Property="Background" Value="{StaticResource ImageToggleButton.MouseOver.Background}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter>
</Style></ResourceDictionary></UserControl.Resources><Border BorderThickness="1" BorderBrush="{Binding BorderBrush,ElementName=uc}"CornerRadius="{Binding CornerRadius,ElementName=uc}"><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition /><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions><Image Source="{Binding IconImage,ElementName=uc}" Margin="4,0"HorizontalAlignment="Center" VerticalAlignment="Center" Height="20" Width="20"/><TextBox Name="tbPlainText" Grid.Column="1" Text="{Binding Password,Mode=TwoWay,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PasswordWithPlainText}}"FontSize="{Binding FontSize,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PasswordWithPlainText}}" TextAlignment="{Binding AlignmentX,ElementName=uc}"BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" Visibility="Collapsed"input:InputMethod.IsInputMethodEnabled="False" Foreground="{Binding Foreground,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PasswordWithPlainText}}"CaretBrush="{Binding CaretBrush,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PasswordWithPlainText}}"><TextBox.Style><Style TargetType="TextBox"><Setter Property="Background" Value="{Binding ElementName=textBox, Path=Background}"/><Style.Triggers><Trigger Property="Text" Value="{x:Null}"><Setter Property="Background" Value="{StaticResource HelpBrush}"/></Trigger><Trigger Property="Text" Value=""><Setter Property="Background" Value="{StaticResource HelpBrush}"/></Trigger></Style.Triggers>
</Style></TextBox.Style></TextBox><PasswordBox Name="pwdCiphertext"  Grid.Column="1" local:PasswordBoxHelper.Attach="True" TextBlock.TextAlignment="{Binding AlignmentX,ElementName=uc}"local:PasswordBoxHelper.Password="{Binding Password,Mode=TwoWay,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PasswordWithPlainText}}"FontSize="{Binding FontSize,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PasswordWithPlainText}}"BorderThickness="0" Background="{StaticResource HelpBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Center"PasswordChanged="pwdCiphertext_PasswordChanged"Foreground="{Binding Foreground,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PasswordWithPlainText}}"CaretBrush="{Binding CaretBrush,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PasswordWithPlainText}}"/><local:ImageToggleButton Grid.Column="2" Width="30"NormalImage="/WpfPasswrod;component/Resources/Eye_50px.png"UnImage="/WpfPasswrod;component/Resources/Invisible_50px.png"Checked="ImageToggleButton_Checked"Unchecked="ImageToggleButton_Unchecked"/></Grid></Border></UserControl>

二、PasswordWithPlainText.cs.xaml 代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfPasswrod.CustomControls
{/// <summary>/// PasswordWithPlainText.xaml 的交互逻辑/// </summary>public partial class PasswordWithPlainText : UserControl{public PasswordWithPlainText(){InitializeComponent();}/// <summary>/// 图标/// </summary>public static readonly DependencyProperty IconImageProperty =DependencyProperty.Register("IconImage", typeof(ImageSource), typeof(PasswordWithPlainText), new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/Resources/Lock_48px.png"))));/// <summary>/// 图标/// </summary>public ImageSource IconImage{get { return (ImageSource)GetValue(IconImageProperty); }set { SetValue(IconImageProperty, value); }}/// <summary>/// 文本框提示文字/// </summary>public string Hint{get { return (string)GetValue(HintProperty); }set { SetValue(HintProperty, value); }}// Using a DependencyProperty as the backing store for Hint.  This enables animation, styling, binding, etc...public static readonly DependencyProperty HintProperty =DependencyProperty.Register("Hint", typeof(string), typeof(PasswordWithPlainText), new PropertyMetadata(null));/// <summary>/// 获取或设置水印背景的水平对齐方式/// </summary>public AlignmentX AlignmentX{get { return (AlignmentX)GetValue(AlignmentXProperty); }set { SetValue(AlignmentXProperty, value); }}// Using a DependencyProperty as the backing store for AlignmentX.  This enables animation, styling, binding, etc...public static readonly DependencyProperty AlignmentXProperty =DependencyProperty.Register("AlignmentX", typeof(AlignmentX), typeof(PasswordWithPlainText), new PropertyMetadata(AlignmentX.Left));/// <summary>/// 获取或设置密码/// </summary>public string Password{get { return (string)GetValue(PasswordProperty); }set { SetValue(PasswordProperty, value); }}// Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc...public static readonly DependencyProperty PasswordProperty =DependencyProperty.Register("Password", typeof(string), typeof(PasswordWithPlainText), new PropertyMetadata(null));/// <summary>/// 获取或设置光标颜色/// </summary>public Brush CaretBrush{get { return (Brush)GetValue(CaretBrushProperty); }set { SetValue(CaretBrushProperty, value); }}// Using a DependencyProperty as the backing store for CaretBrush.  This enables animation, styling, binding, etc...public static readonly DependencyProperty CaretBrushProperty =DependencyProperty.Register("CaretBrush", typeof(Brush), typeof(PasswordWithPlainText), new PropertyMetadata(Brushes.Black));/// <summary>/// 获取或设置光标颜色/// </summary>public Brush BorderBrush{get { return (Brush)GetValue(BorderBrushProperty); }set { SetValue(BorderBrushProperty, value); }}// Using a DependencyProperty as the backing store for CaretBrush.  This enables animation, styling, binding, etc...public static readonly DependencyProperty BorderBrushProperty =DependencyProperty.Register("BorderBrush", typeof(Brush), typeof(PasswordWithPlainText), new PropertyMetadata(Brushes.Black));public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(PasswordWithPlainText),new PropertyMetadata(new CornerRadius(0, 0, 0, 0), OnCornerRadiusChanged));public CornerRadius CornerRadius{get { return (CornerRadius)GetValue(CornerRadiusProperty); }set{SetValue(CornerRadiusProperty, value);}}private static void OnCornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){CornerRadius cornerRadius = new CornerRadius();cornerRadius = (CornerRadius)e.NewValue;}/// <summary>/// 切换成明文/// </summary>/// <param name="sender"></param>/// <param name="e"></param>/// private void ImageToggleButton_Checked(object sender, RoutedEventArgs e){pwdCiphertext.Visibility = Visibility.Collapsed;tbPlainText.Visibility = Visibility.Visible;}/// <summary>/// 切换成密文/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void ImageToggleButton_Unchecked(object sender, RoutedEventArgs e){pwdCiphertext.Visibility = Visibility.Visible;tbPlainText.Visibility = Visibility.Collapsed;}private void pwdCiphertext_PasswordChanged(object sender, RoutedEventArgs e){PasswordBox pwd = sender as PasswordBox;if (pwd != null){if (!string.IsNullOrEmpty(pwd.Password)){pwd.Background = Brushes.Transparent;}else{pwd.Background = (Brush)FindResource("HelpBrush");}}}}
}

源码地址

github:https://github.com/yanjinhuagood/WPFDevelopers.git

gitee:https://gitee.com/yanjinhua/WPFDevelopers.git

WPF开发者QQ群: 340500857 

blogs: https://www.cnblogs.com/yanjinhua

Github:https://github.com/yanjinhuagood

出处:https://www.cnblogs.com/yanjinhua

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请著名作者 出处 https://github.com/yanjinhuagood

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/301572.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

麻省理工学院迷你猎豹机器人,四条腿都能后空翻了,又是黑科技啊~

全世界只有3.14 % 的人关注了数据与算法之美麻省理工学院公布了一款迷你猎豹机器人&#xff0c;是第一个做后空翻的四足机器人。只有20磅的四肢四足动物可弯曲并摆动腿部&#xff0c;使其能够向右或向上行走。机器人也可以在不平坦的地形上小跑&#xff0c;大约是普通人行走速度…

c语言宏高级用法,C语言宏高级用法 [总结]

1、前言今天看代码时候&#xff0c;遇到一些宏&#xff0c;之前没有见过&#xff0c;感觉挺新鲜。如是上网google一下&#xff0c;顺便总结一下&#xff0c;方便以后学习和运用。C语言程序中广泛的使用宏定义&#xff0c;采用关键字define进行定义&#xff0c;宏只是一种简单的…

Win 11 真的要来了!微软宣布 Win10 将于 2025 年终止支持!

微软今日正式更新支持页面&#xff0c;Windows 10 家庭版和专业版的支持将于 2025 年 10 月 14 日结束。值得注意的是&#xff0c;这是微软首次描述对 Windows 10 的支持结束&#xff0c;在此之前&#xff0c;微软仅记录了特定的 Windows 10 正式版本&#xff08;如最新的 Wind…

每日一笑 | 为什么椅子总是最乱的?

全世界只有3.14 % 的人关注了数据与算法之美&#xff08;图片来源于网络&#xff0c;侵权删&#xff09;

R,让你的数据分析更简便!

R语言被广泛应用于生物、医学、电商、新闻等数据相关行业&#xff0c;是目前主流数据应用软件之一。它是用来进行统计分析、绘图和统计编程的强大工具&#xff0c;它在大规模的数据挖掘、可视化和报告方面已经取得了巨大的成功。你能够轻松地获取各种的包&#xff08;通过 CRAN…

久坐伤腰!这款德国3D美臀坐垫,分散身体压力,保护腰椎尾椎。

▲数据汪特别推荐点击上图进入玩酷屋一直听闻德国是一个「直肠子」国家他们的严谨、认真、一丝不苟让这个只有8000万人口的国家竟有2300多个享誉全球的名牌小到牙刷牙线&#xff0c;大到汽车「德国制造」已成为高品质的代名词德国品质到底是一种怎样的存在&#xff1f;德国人生…

C sharp实例:华盾武器门数据接收和解析

项目介绍&#xff1a;本实例主要是接收安检金属门的数据解析并显示到界面上&#xff0c;只做功能实现&#xff0c;不做界面美化硬件&#xff1a;金属门一个、网线一根、电脑主机&#xff0c;金属门网线可以直接接到电脑主机上开发环境&#xff1a;vs2017 系统&#xff1a;win10…

Nature封面:大团队日趋中庸,小团队更容易出颠覆性创新

全世界只有3.14 % 的人关注了数据与算法之美【导读】现代科学研究团队的规模越来越大&#xff0c;一般认为&#xff0c;大团队往往有着更充足的经费支持和多样化的研究人员&#xff0c;更有利于科学创新。但近日Nature发表封面文章&#xff0c;认为大团队往往因为失败代价太高而…

linux 创建ll,两台linux建立GRE隧道

1.拓扑图&#xff1a; 备注&#xff1a;因为应用原因&#xff0c;需要在linux2上添加一个公网地址&#xff0c;并且在中间路由设备不受控制的情况下&#xff0c;Linux1能访问到linux2上面的公网地址。2.基本接口配置&#xff1a;linux1:192.168.10.1/24linux2:192.168.20.2/24R…

Exchange Server2010系列之十一:部署CAS+HT角色高可用性

Exchange Server 2010使用基于角色的管理模型。多角色服务器的安装及配置&#xff0c;我们在前面的文章中已经做过介绍。本案例中根据高可用性要求&#xff0c;我们需要把服务器角色部署在分开的服务器上面&#xff0c;并且实现CAS、HT、MBX角色的高可用。在此&#xff0c;我的…

用BenchmarkDotNet看Property

属性——Property&#xff0c;由get,set访问器组成&#xff0c;这是C#使用度比较高的类成员。今天分几组对比测试&#xff0c;来看一下使用Property的性能。被测试对象&#xff1a;public class MyClass {private string _myProperty1 DateTime.Now.ToString();public string …

盘点2019年最有钱的爸爸们

全世界只有3.14 % 的人关注了数据与算法之美纽约时间3月5日&#xff0c;又到了一年一度的大佬炫富时间了&#xff0c;福布斯发布第33期2019年度全球亿万富豪榜&#xff0c;全世界最有钱的人都在这了。第33期福布斯全球亿万富豪榜TOP20 ↓据统计&#xff0c;本次评选一共有2153位…

android拖拽 字体变形,字体变形的几种方法与技巧

原标题&#xff1a;字体变形的几种方法与技巧替换法是在统一形态的文字元素加入另类不同的图形元素或文字元素。 其本质是根据文字的内容意思&#xff0c;用某一形象替代字体的某个部分或某一笔画&#xff0c;这些形象或写实或夸张。将文字的局部替换&#xff0c;是文字的内涵外…

每日一笑 | 终于知道为什么胖了之后气质会垮了

全世界只有3.14 % 的人关注了数据与算法之美&#xff08;图片来源于网络&#xff0c;侵权删&#xff09;

人工智能时代,学好数学很重要!

随着科技的快速发展&#xff0c;人工智能的重要性日渐显现。而数学知识蕴含着处理智能问题的基本思想与方法&#xff0c;是理解复杂算法的必备要素。在机器学习工作流程中&#xff0c;数学与代码高度交织在一起&#xff0c;代码通常可以根据数学直观地构建&#xff0c;甚至会共…

为什么选择ASP.NET Core

什么是.NET有一次小飞去面试&#xff0c;面试官上来就问了一个宏观的问题&#xff1a;“你觉得什么是.NET”&#xff1f;小飞的脑子嗡嗡的&#xff0c;支吾了半天&#xff0c;才吐了一些碎片化的词语&#xff1a;“跨平台、开源、微软…”虽然作为一个.NET人&#xff0c;但是小…

抢不到“猫抓杯”怎么办?这些女神更喜欢

▲数据汪特别推荐点击上图进入玩酷屋不久前才送走了西方情人节&#xff0c;男同胞们的第二个“送礼节”----女神节。这个令所有女生翘首、期待、憧憬、摩拳擦掌的日子&#xff0c;令所有男士小心翼翼、挑礼物到崩溃的日子&#xff0c;就要到了。小木作为妇女之友&#xff0c;听…

程序出Bug,一定是环境的锅!

现象前几天&#xff0c;一个客户反映说&#xff0c;他看到的数据顺序是乱的&#xff0c;不是按照日期倒排。但是在测试环境、预生产环境&#xff0c;都无法重现问题&#xff0c;即使使用相同的程序版本。定位查看代码&#xff0c;出问题的功能使用了第三方ORM框架&#xff0c;无…

AIX逻辑卷管理(LVM)

VG PV PP LV LP关系图 转载于:https://www.cnblogs.com/limt/archive/2013/04/06/4199257.html

鸿蒙操作系统如何打通 Windows 操作系统?

华为&#xff1a;我们选择拥抱 Windows华为给出的解决方案是在微软 Windows 操作系统中部署 HarmonyOS 分布式插件。运行 Windows 操作系统的电脑和运行鸿蒙操作系统的华为手机将形成超级终端&#xff0c;文件资源管理器实现互通。在 Windows 文件资源管理器中&#xff0c;将显…