Search code examples
c#uwpplaceholderpasswordbox

PasswordBox placeholder text isn't returning when cleared


So I have a PasswordBox:

<PasswordBox x:Name="pwbPassword" PlaceholderText="Password" 
             Password="{x:Bind Path=local:Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

However I also have a clear function which contains:

Password = null

The issue I am having is that the PlaceholderText isn't being reinstated and instead the box is left blank.

Is this a bug or am I missing somthing?


Solution

  • PasswordBox placeholder text isn't returning when cleared

    I check your code, Password is string property, please bind with string field.

    <PasswordBox
    
        Name="MyPassswordBox"
        Height="44"
        MaxLength="20"
        Password="{x:Bind PassWord, Mode=TwoWay}"
        PlaceholderText="Input your Password"
        />
    

    Code behind

    private string _passWord;
    
     public event PropertyChangedEventHandler PropertyChanged;
     private void OnPropertyChanged([CallerMemberName] string propertyName = null)
     {
         if (PropertyChanged != null)
         {
             this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }
     }
     public string PassWord
     {
    
         get { return _passWord; }
         set
         {
             _passWord = value;
             OnPropertyChanged();
    
         }
     }
    

    If you want clear password, please set PassWord as null.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        PassWord = null;
    }
    

    I have tested, the PlaceholderText will be reinstated.