WPF password加水印

1、增加扩展属性

public static class PasswordHelper
{
	public static readonly DependencyProperty PasswordProperty =
		DependencyProperty.RegisterAttached("Password",
		typeof(string), typeof(PasswordHelper),
		new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));

	public static readonly DependencyProperty AttachProperty =
		DependencyProperty.RegisterAttached("Attach",
		typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));

	private static readonly DependencyProperty IsUpdatingProperty =
		DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
		typeof(PasswordHelper));


	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;
			SetPasswordLength(passwordBox, passwordBox.Password.Length);
		}
		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;
		if (passwordBox == null)
		{
			return;
		}

		SetPasswordLength(passwordBox, passwordBox.Password.Length);
		SetIsUpdating(passwordBox, true);
		SetPassword(passwordBox, passwordBox.Password);
		SetIsUpdating(passwordBox, false);
	}


	public static bool GetIsMonitoring(DependencyObject obj)
	{
		return (bool)obj.GetValue(IsMonitoringProperty);
	}

	public static void SetIsMonitoring(DependencyObject obj, bool value)
	{
		obj.SetValue(IsMonitoringProperty, value);
	}

	public static readonly DependencyProperty IsMonitoringProperty =
		DependencyProperty.RegisterAttached("IsMonitoring", typeof(bool), typeof(PasswordHelper), new UIPropertyMetadata(false, OnIsMonitoringChanged));



	public static int GetPasswordLength(DependencyObject obj)
	{
		return (int)obj.GetValue(PasswordLengthProperty);
	}

	public static void SetPasswordLength(DependencyObject obj, int value)
	{
		obj.SetValue(PasswordLengthProperty, value);
	}

	public static readonly DependencyProperty PasswordLengthProperty =
		DependencyProperty.RegisterAttached("PasswordLength", typeof(int), typeof(PasswordHelper), new UIPropertyMetadata(0));

	private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	{
		var pb = d as PasswordBox;
		if (pb == null)
		{
			return;
		}
		if ((bool)e.NewValue)
		{
			pb.PasswordChanged += PasswordChanged;
		}
		else
		{
			pb.PasswordChanged -= PasswordChanged;
		}
	}
}

2、方法一,使用VisualBrush, namespace.Classes为PasswordHelper所在名称空间

xmlns:cls="clr-namespace:namespace.Classes"

<PasswordBox FontSize="15" Height="25" HorizontalAlignment="Left" Margin="115,158,0,78" Name="passwordBox1"  VerticalAlignment="Center" Width="179" 
			 Grid.ColumnSpan="2" 
			 cls:PasswordHelper.Attach="True" cls:PasswordHelper.Password="{Binding Path=UserInfo.UserPassword, Mode=TwoWay}">
	<PasswordBox.Resources>
		<VisualBrush x:Key="pwdBrush" TileMode="None" Opacity="0.3" Stretch="None" AlignmentX="Left">
			<VisualBrush.Visual>
				<TextBlock FontStyle="Italic" Text="请输入密码"/>
			</VisualBrush.Visual>
		</VisualBrush>
	</PasswordBox.Resources>
	<PasswordBox.Style>
		<Style TargetType="PasswordBox">
			<Style.Triggers>
				<Trigger Property="cls:PasswordHelper.Password" Value="{x:Null}">
					<Setter Property="Background" Value="{StaticResource pwdBrush}"/>
				</Trigger>
				<Trigger Property="cls:PasswordHelper.Password" Value="">
					<Setter Property="Background" Value="{StaticResource pwdBrush}"/>
				</Trigger>
			</Style.Triggers>
		</Style>
	</PasswordBox.Style>
</PasswordBox>

3、法二,使用ControlTemplate

<Window.Resources>
	<ControlTemplate x:Key="WaterMarkTemplate" TargetType="{x:Type PasswordBox}">
		<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
			<Grid>
				<ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
				<TextBlock x:Name="WaterMark" Focusable="False" Visibility="Collapsed" FontStyle="Italic" Text="请输入密码" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Opacity="0.5"/>
			</Grid>
		</Border>
		<ControlTemplate.Triggers>
			<Trigger Property="cls:PasswordHelper.PasswordLength" Value="0">
				<Setter TargetName="WaterMark" Property="Visibility" Value="Visible"/>
			</Trigger>
		</ControlTemplate.Triggers>
	</ControlTemplate>		
</Window.Resources>
<PasswordBox FontSize="15" Height="25" HorizontalAlignment="Left" Margin="115,158,0,78" Name="passwordBox1"  VerticalAlignment="Center" Width="179" 
	 Grid.ColumnSpan="2" Template="{StaticResource WaterMarkTemplate}"
	 cls:PasswordHelper.Attach="True" cls:PasswordHelper.Password="{Binding Path=UserInfo.UserPassword, Mode=TwoWay}">
</PasswordBox>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值