Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: MVVM - disable button if command is null

I have binding on some command:

<Button Command="{Binding Save}" />

Save is command of some object that can be selected from list. In initial state there is no any selected object so binding does not work and CanExecute does not invoked. How can i disable this button using MVVM?

Solution: WPF/MVVM: Disable a Button's state when the ViewModel behind the UserControl is not yet Initialized?

Guys, thanks for your answers and sorry for duplication of question.

like image 542
Rover Avatar asked Mar 03 '11 12:03

Rover


1 Answers

Define a command that always return false to CanExecute. Declare it at a global position such as in your App.Xaml. you can specify this empty-command then as the FallbackValue for all your command bindings you expect a null value first.

<Button Command="{Binding Save,FallbackValue={StaticResource KeyOfYourEmptyCommand}}" /> 
like image 189
HCL Avatar answered Oct 28 '22 12:10

HCL