CanExecute and CanExecuteChanged, I must implement these with a RelayCommand?

Martin picture Martin · May 3, 2011 · Viewed 12.7k times · Source

I am using MVVM-Light and i have my relay command working perfectly, I have just read that i should be implementing CanExecuteChanged and CanExecute. Although i am unable to find a good example.

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

CanExecute needs to return False when it can't be executed but wouldn't just disbale the button ??

When do i execute the CanExecuteChanged?

Anyone have any good examples of when to use each one, my code works without but this blog post states that I should be implementing these items.

I am a little confused, as I said I thought I would just bind the Enabled property or something to a property in the ViewModel so I can disable the button or a similar control?

Any help in understanding would be really grateful.

EDIT

This is what i have now... Its working but the button isn't physically DISABLED only the commmand doesn't run as i am returning false. I am calling CanExecuteMe in the constructor to force the RaiseCanExecuteChanged to run ...

This runs in my construtor of my viewmodel

        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>

Answer

user1228 picture user1228 · May 3, 2011

CanExecute is called when the Button needs to determine if it should be enabled or not.

The Button does this on binding, and after every time CanExecuteChanged fires (the Button listens to this event for its Command).

So, if the button should be disabled, you should fire CanExecuteChanged and, when the button calls CanExecute, you should return false. This is the preferred method of enabling/disabling a button when using command bindings.

Command bindings enable you to encapsulate all the button logic within an instance (the Command). The CanExecute method should 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 over there and I forgot about that one down there.