views:

100

answers:

3

I'm trying to get a grasp on WPF and MVVM and have been making good progress. The WPF and MVVM side of things are going well.

However, the XAML and data binding side is a whole other story :)

How would I go about "disabling" a button?

For example, I have a CanClose property in my view model that determines whether or not the application can currently be closed. If a worker thread is off doing something, then this property is set to false and I'd like to either grey out the button or somehow visually disable the Close button via some sort of binding.

How would I go about doing this?

Thanks!

Edit -

Too bad I can only accept one answer.

These two answers helped me tremendously. In Kent's post, he went a step further by explaining why you should implement a command infrastructure in your application instead of disabling a button in the way that I had asked:

http://stackoverflow.com/questions/3476686/how-does-one-disable-a-button-in-wpf-using-the-mvvm-pattern/3476708#3476708

And the answer to my original question:

http://stackoverflow.com/questions/3476686/how-does-one-disable-a-button-in-wpf-using-the-mvvm-pattern/3476697#3476697

+3  A: 

Just bind the IsEnabled property of the Button to CanClose:

<Button IsEnabled="{Binding CanClose}"/>
Quartermeister
No way, is it really that easy? I have no idea how I could've overlooked that. Let me try it out.
Ian P
Thanks, I can't believe I overlooked such an easy answer to the problem.
Ian P
+3  A: 

If you return CanExecute of ICommand a value of false, then Button will be disabled. So whatever command your button is bound to, see if you can return CanExecute a value of false when you want to disable it.

Akash Kava
+6  A: 

By way of using the command pattern. In your view model:

public class MyViewModel : ViewModel
{
    private readonly ICommand someCommand;

    public MyViewModel()
    {
        this.someCommand = new DelegateCommand(this.DoSomething, this.CanDoSomething);
    }

    public ICommand SomeCommand
    {
        get { return this.someCommand; }
    }

    private void DoSomething(object state)
    {
        // do something here
    }

    private bool CanDoSomething(object state)
    {
        // return true/false here is enabled/disable button
    }
}

In your XAML:

<Button Command="{Binding SomeCommand}">Do Something</Button>

Read this post to find out more about the DelegateCommand.

HTH,
Kent

Kent Boogaart
Nice link, thanks for the info. I'll read the whole thing this afternoon when I get some additional free time. I take it DelegateCommand is your own implementation of the command pattern, or is this something in the .net framework that I'm missing?
Ian P
@IanP DelegateCommand is part of Prism which seems to be the de facto way of writing WPF apps these days....
BFree
Hmm.. Without knowing what DelegateCommand does, this doesn't help me as much as I had hoped.. lol
Ian P
Ah -- Kent has a link to a sample DelegateCommand implementation on his blog. Thanks again!
Ian P