jololandayan
jololandayan

Reputation: 55

How to disable button in wpf

I have my button binded into my ViewModel with the property of IsEnabled to have it true or false from my ViewModel but it does not make it disabled whenever I set the property binded to false.

My XAML

<Button x:Name="buttonSubmit" Margin="20,10,0,0" Height="30" Width="90" Content="Login" IsEnabled="{Binding IsLoginEnabled, Mode=TwoWay}" Command="{Binding LoginCommand}" CommandParameter="{Binding ElementName=txtPassword}"/>

ViewModel

public LoginViewModel(ILoginAuth loginAuth)
    {
        this.IsLoginEnabled = true;
        this.LoginCommand = new DelegateCommand(this.LoginUser);
    }

 public async void LoginUser()
    {
            this.IsLoginEnabled = false;
    }

Upvotes: 0

Views: 2522

Answers (2)

Clemens
Clemens

Reputation: 128042

You don't typically bind IsEnabled when you've also bound the Command property. The CanExecute method of the ICommand object controls whether the Button is enabled or not:

public DelegateCommand LoginCommand { get; }
private bool canLogin = true;

public LoginViewModel(ILoginAuth loginAuth)
{
    LoginCommand = new DelegateCommand(LoginUser, () => canLogin);
}

public void LoginUser()
{
    canLogin = false;
    LoginCommand.RaiseCanExecuteChanged();
}

Upvotes: 2

Coops
Coops

Reputation: 301

My first guess would be that you're not implementing INotifyPropertyChanged in your ViewModel.

Check these links out:

Explain INotifyPropertyChanged In WPF - MVVM

How to: Implement the INotifyPropertyChanged Interface

You need to implement the interface in order for the ViewModel to inform the View that something has changed and update the UI accordingly.

Upvotes: 1

Related Questions