Button disbale and enable PassowrdBox Using MVVM

619 Views Asked by At

How to disable q button if a password box is not filled? Let's say if username and password fields are empty button should be disable as fill both fields username and password then button should be enable.

1

There are 1 best solutions below

0
On

Just bind the IsEnabled property of the Button and use the bound property getter to determine if true or false. For example:

XAML

<Button IsEnabled="{Binding IsUsernameAndPasswordValid }"/>

C#

public partial class exampleWindow : Window 
{
     public exampleWindow () 
     {
          InitializeComponent();
          DataContext = this;      
     }

     public bool IsUsernameAndPasswordValid
     {
         get { return (String.IsNullOrEmpty(UsernameText) && String.IsNullOrEmpty(PasswordText)) ; }
     }
}

And dont forget to set the data context