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();
        }
    }
}
相关推荐
明耀7 小时前
WPF DataGrid 默认显示行号
wpf
lph197211 小时前
wpf的converter
wpf
fyifei055811 小时前
WPF学习PropertyChanged
wpf
爱炸薯条的小朋友11 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
baivfhpwxf202312 小时前
wpf ListBox 去除item 单击样式
wpf
诗仙&李白12 小时前
lnnovationHubTool,用prism+WPF编写的MVVM模式的快速上位机软件开发框架平台
wpf·mvvm·prism·上位机软件开发框架平台
程序员小刘15 小时前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
Java Fans1 天前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽2 天前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf
code bean3 天前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf