CanExecuteChanged event not raised.

May 12, 2010 at 1:41 PM
I'm useing MVVM light toolkit for WPF. It looks like CanExecuteChanged isn't raised at all. Even when I call: RaiseCanExecuteChanged().
Coordinator
May 21, 2010 at 9:25 AM

Hi,

In WPF, the CanExecute method is called by the CommandManager class. The RaiseCanExecuteChanged method is present in WPF for compatibility with Silverlight, where there is no CommandManager and the CanExecuteChanged event must be raised manually.

In a previous version, I had offered the possibility to force the CanExecuteChanged event to be raised, but it created some issues where the CanExecute method was executed twice. 

Do you have a scenario where the CanExecute method is not called by the CommandManager?

Thanks,

Laurent

Sep 1, 2010 at 1:59 AM

Actually Laurent I think I'm getting this situation too, a contrived example would be as follows:

 

Snippet

            CloseCommand = new RelayCommand(
                () => MessageBox.Show("I don't wanna close! Give up!"),
                () =>
                {
                    if (DateTime.Now.Second % 3 == 0)
                    {
                        return true;
                    }
                    return false;
                });

            t = new System.Timers.Timer(10);
            t.Elapsed += (s, e) =>
                {
                    CloseCommand.RaiseCanExecuteChanged();
                };
            t.Start();

You would imagine that every 10ms it would re-evaluate and update the visual for CloseCommand's bound controls, but this is not the case in WPF.