user496949
user496949

Reputation: 86075

How to disable a button dynamically

I want to gray out a button dynamically. How to do that?

Upvotes: 3

Views: 5446

Answers (3)

Arseny
Arseny

Reputation: 7351

here id more sophisticated way (WPF way) is to bind Command to the Button.

 <Button Name="button1" VerticalAlignment="Top" Width="94" Command="{Binding MyCommand}"

in ViewModel which is bound to the view's dataContext:

public ICommand MyCommand
    {
        get
        {
           return  new DelegateCommand<string>(ExecuteSomething,IsExecutable);
        }
    }

here is ExecuteSomething the method which will be executed by clicking your button IsExecutable - perdicate while it returns false the Button will be disable

Upvotes: 0

kiwipom
kiwipom

Reputation: 7709

in xaml

<Button Name="myButton">Click Me</Button>

in code behind

myButton.IsEnabled = false;

Upvotes: 11

Joey
Joey

Reputation: 354386

Set its IsEnabled property to false. You can do this either in code-behind or with triggers/styles, depending on your needs there.

Upvotes: 1

Related Questions