2

In my XAML I have:

<PasswordBox PlaceholderText="Password" Name="login_password" Width="350" />

Am trying to check if the PasswordBox is empty via:

if(login_password.Text == "") //this returns an error
{
    //show message box
}

How can I check if the PasswordBox is empty?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109

1 Answers1

3

The PasswordBox has the Password property. So you should check for Password property instead of Text:

if(login_password.Password == "")
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 2
    When checking if a string has a value, you should use `string.IsNullOrWhitespace(login_password.Password)`. This checks if the string is `null`, empty (`""`) or only contains whitespace characters, such as spaces or newlines – Jon G Stødle Jul 10 '17 at 08:48
  • @JonStødle Good point. I'm agree with you and here is an example also https://stackoverflow.com/a/32631216/2946329 – Salah Akbari Jul 10 '17 at 08:57