How to bind to a PasswordBox in MVVM

前端 未结 30 2101
执念已碎
执念已碎 2020-11-22 11:50

I have come across a problem with binding to a PasswordBox. It seems it\'s a security risk but I am using the MVVM pattern so I wish to bypass this. I found som

30条回答
  •  醉梦人生
    2020-11-22 12:38

    Its very simple . Create another property for password and Bind this with TextBox

    But all input operations perform with actual password property

    private string _Password;

        public string PasswordChar
        {
            get
            {
                string szChar = "";
    
                foreach(char szCahr in _Password)
                {
                    szChar = szChar + "*";
                }
    
                return szChar;
            }
    
            set
            {
                _PasswordChar = value; NotifyPropertyChanged();
            }
        }
    

    public string Password { get { return _Password; }

            set
            {
                _Password = value; NotifyPropertyChanged();
                PasswordChar = _Password;
            }
        }
    

提交回复
热议问题