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();
        }
    }
}
相关推荐
公子小六2 小时前
在WPF程序中实现PropertyGrid功能
windows·microsoft·c#·.net·wpf
当下就是最好1 天前
WPF应用程序的生命周期-笔记
wpf
九鼎科技-Leo2 天前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
麻花20132 天前
C#之WPF的C1FlexGrid空间的行加载事件和列事件变更处理动态加载的枚举值
开发语言·c#·wpf
lcintj2 天前
【WPF】Prism学习(九)
学习·wpf·prism
界面开发小八哥2 天前
界面控件DevExpress WPF中文教程:网格视图数据布局的列和卡片字段
wpf·界面控件·devexpress·ui开发·用户界面
△曉風殘月〆2 天前
如何在WPF中嵌入其它程序
wpf
Crazy Struggle2 天前
功能齐全的 WPF 自定义控件资源库(收藏版)
.net·wpf·ui控件库
shepherd枸杞泡茶3 天前
WPF动画
c#·.net·wpf
lcintj3 天前
【WPF】Prism学习(十)
学习·wpf·prism