WPF PasswordBox

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfDemo
{
    public class ZenViewModel:INotifyPropertyChanged
    {
		private string userName;

		public string UserName
		{
			get { return userName; }
			set { 
				userName = value;
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("UserName"));
            }
		}
		private string password;

		public string Password
		{
			get { return password; }
			set { 
				password = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Password"));
            }
		}

		public event PropertyChangedEventHandler? PropertyChanged;
    }
}
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfDemo
{
    public class ZenPasswordBoxHelper
    {


        public static string GetZenPassword(DependencyObject obj)
        {
            return (string)obj.GetValue(ZenPasswordProperty);
        }

        public static void SetZenPassword(DependencyObject obj, string value)
        {
            obj.SetValue(ZenPasswordProperty, value);
        }

        // Using a DependencyProperty as the backing store for ZenPassword.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ZenPasswordProperty =
            DependencyProperty.RegisterAttached("ZenPassword", typeof(string), typeof(ZenPasswordBoxHelper), new PropertyMetadata("",OnZenPropertyChangedCallback));

        private static void OnZenPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is PasswordBox)
            {
                PasswordBox pwdBox = (PasswordBox)d;
                pwdBox.Password = (string)e.NewValue;
                Console.WriteLine("AAA    NewValue---" + (string)e.NewValue+ "--OldValue--" + (string)e.NewValue);
            }
            else           
            return;
        }

        public static bool GetIsEnableBind(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsEnableBindProperty);
        }

        public static void SetIsEnableBind(DependencyObject obj, bool value)
        {
            obj.SetValue(IsEnableBindProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsEnableBind.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEnableBindProperty =
            DependencyProperty.RegisterAttached("IsEnableBind", typeof(bool), typeof(ZenPasswordBoxHelper), new PropertyMetadata(false, OnPropertyChangedCallback));

        private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

            if (d is PasswordBox)
            {
                PasswordBox pwdBox = (PasswordBox)d;
                if ((bool)e.NewValue)
                {
                    pwdBox.PasswordChanged += PwdBox_PasswordChanged;
                }
              else
                {
                    pwdBox.PasswordChanged -= PwdBox_PasswordChanged;
                }
                Console.WriteLine("BBB    NewValue--"+e.NewValue.ToString()+ "--OldValue--" + e.OldValue.ToString());
            }
            
        }

        private static void PwdBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
           PasswordBox pwdBox = (PasswordBox)sender;
            SetZenPassword(pwdBox, pwdBox.Password);
        }
    }
}
csharp 复制代码
using System.Text;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ZenViewModel vm;
        public MainWindow()
        {
           
            InitializeComponent();
            vm = new ZenViewModel();
            this.DataContext = vm;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            vm.Password = DateTime.Now.ToString();
            vm.UserName = DateTime.Now.ToString();
        }
    }
}
相关推荐
wangnaisheng2 天前
【WPF】Opacity 属性的使用
wpf
姬激薄2 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)2 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心2 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo3 天前
9.1.领域驱动设计
wpf
大道随心3 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠3 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go3 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet4 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf
冰茶_4 天前
WPF之绑定模式深入
学习·microsoft·微软·c#·wpf·绑定模式