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();
        }
    }
}
相关推荐
假男孩儿4 小时前
WPF 最小化到系统托盘
wpf
勇敢小菜鸟15 小时前
WPF自定义窗口 输入验证不生效
wpf
鲤籽鲲15 小时前
WPF TextBox 输入限制 详解
wpf
鸿喵小仙女15 小时前
C# WPF读写STM32/GD32单片机Flash数据
stm32·单片机·c#·wpf
六点的晨曦15 小时前
WPF的右键菜单项目引入DLL和DllImport特性引入DLL文件的异同点
wpf
一个不正经的林Sir15 小时前
C#WPF基础介绍/第一个WPF程序
开发语言·c#·wpf
可喜~可乐1 天前
C# WPF开发
microsoft·c#·wpf
界面开发小八哥1 天前
DevExpress WPF中文教程:Grid - 如何移动和调整列大小?(二)
ui·.net·wpf·界面控件·devexpress·ui开发
界面开发小八哥1 天前
「实战应用」如何用图表控件SciChart WPF实现应用程序的DPI感知?
信息可视化·wpf·数据可视化·图表·scichart wpf·scichart
明耀3 天前
WPF TabControl 设置item不能点击
wpf