Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one "disable" a button in WPF using the MVVM pattern?

Tags:

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:

How does one "disable" a button in WPF using the MVVM pattern?

And the answer to my original question:

How does one "disable" a button in WPF using the MVVM pattern?

like image 822
Ian P Avatar asked Aug 13 '10 12:08

Ian P


People also ask

What is MVVM command?

Commands are an implementation of the ICommand interface that is part of the . NET Framework. This interface is used a lot in MVVM applications, but it is useful not only in XAML-based apps.

Do I have to use MVVM in WPF?

The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern. Though it is possible to create WPF applications without using the MVVM pattern, a little investment in learning can make building WPF applications much simpler.


1 Answers

Just bind the IsEnabled property of the Button to CanClose:

<Button IsEnabled="{Binding CanClose}"/> 
like image 74
Quartermeister Avatar answered Sep 23 '22 08:09

Quartermeister