CanExecute and CanExecuteChanged, should I implement them using RelayCommand?

I am using MVVM-Light and I am working great on my relay team, I just read that I have to implement CanExecuteChanged and CanExecute . Although I can not find a good example.

Does anyone have a good example of how to implement them.

CanExecute should return False when it cannot be executed, but the button will not be simply disbale

When do I execute CanExecuteChanged ?

Someone has good examples of when to use each, my code works, but this blog says that I have to follow these points.

I'm a bit confused as I said that I thought I would just bind the Enabled property or something to the property in the ViewModel so that I can disable the button or similar control?

Any help in understanding would be greatly appreciated.

EDIT

This is what I have now ... Its working, but the button is not physically disabled, only commmand does not start, since I return false. I call CanExecuteMe in the constructor to make RaiseCanExecuteChanged work ...

This works in my constructor for my view model

  this.Page2Command = new RelayCommand(() => this.GoToPage2(), () => CanExecuteMe); CanExecuteMe = false; 

and here is the rest of my code, I took it from an example.

  private bool _canIncrement = true; public bool CanExecuteMe { get { return _canIncrement; } set { if (_canIncrement == value) { return; } _canIncrement = value; // Update bindings, no broadcast //RaisePropertyChanged(CanIncrementPropertyName); Page2Command.RaiseCanExecuteChanged(); } } public RelayCommand Page2Command { get; private set; } private object GoToPage2() { System.Windows.MessageBox.Show("Navigate to Page 2!"); return null; } 

And here is my xaml

  <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="31,77,0,0" x:Name="button1" VerticalAlignment="Top" Width="75" > <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Page2Command, Mode=OneWay}"/> </i:EventTrigger> </i:Interaction.Triggers> </Button> 
+7
source share
2 answers

Called by CanExecute when a Button needs to determine whether it should be enabled or not.

The button does this when binding, and after each start of FireFox FireWire (the button listens for this event for its team).

So, if the button should be disabled, you should run CanExecuteChanged and, when the button calls CanExecute, you should return false . This is the preferred way to enable / disable the button when using command bindings.

Command bindings allow you to encapsulate the entire logic of buttons in an instance (command). The CanExecute method must query the current state of the application to determine if the button should be enabled or disabled. By this encapsulation, you reduce the spaghetti code in your view model, where these checks are performed here and there and there, and I forgot about it.

+10
source

You must be very careful when using the CanExecute predicate. It checks every change in the user interface and every keyboard key that is entered in ANY field.

This can cause performance problems!

+1
source

All Articles