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

2

7

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>
Haematothermal answered 3/5, 2011 at 13:33 Comment(0)
A
10

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.

Annieannihilate answered 3/5, 2011 at 13:56 Comment(3)
Thanks will, Ok i have it working and sure enought the Button doesn't execute as it returns false. But the button is "STILL" enabled... I will update my question with my code.. Maybe i am doing something wrong.Haematothermal
Ok found it, there is a property called MustToggleIsEnabledValue that i must set.. I will mark this question as answered as this reply pretty much answered my question.Haematothermal
@Haematothermal you should put the bit about MustToggleIsEnabled in the question edit, I just spent an hour trying to figure it outBrittnee
M
1

You should be very careful using the CanExecute predicate. It checks on every UI change and for every keyboard key entered into ANY field.

This can cause performance issues!

Macron answered 27/6, 2013 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.