Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm certain that this is a stupidly simple oversight on my part, but I can't get it to work. I have a button with a command property bound to a DelegateCommand that will not enable when I want.

Pretty standard DelegateCommand class
C#
public class DelegateCommand : ICommand
 {
     public event EventHandler CanExecuteChanged;

     Func<object, bool> canExecute;
     Action<object> executeAction;
     bool canExecuteCache;

     public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
     {
         this.executeAction = executeAction;
         this.canExecute = canExecute;
     }

     public bool CanExecute(object parameter)
     {
         bool temp = canExecute(parameter);

         if (canExecuteCache != temp)
         {
             canExecuteCache = temp;
             if (CanExecuteChanged != null)
             {
                 CanExecuteChanged(this, new EventArgs());
             }
         }
         return canExecuteCache;
     }

     public void Execute(object parameter)
     {
         executeAction(parameter);
     }
 }


Standard declaration in my view model

C#
public DelegateCommand ArchiveData { get; private set; }


In the view model's constructor I have
C#
ArchiveData = new DelegateCommand(m => Cmds.ArchiveSelectedData(this), y => CanArchive());


Finally, I have a CanExecute of
C#
private bool CanArchive()
{
    return (DatesPresent || OffSeasonForm) || (PlayerNotes || TravelPlans);
}


The line
C#
return (DatesPresent || OffSeasonForm) || (PlayerNotes || TravelPlans);

refers to 4 bool properties of the view model, bound to the IsChecked properties of 4 different checkboxes. Each bool is a notify property changed property and behaves as expected.

The goal is to have the button disabled until one or more of the checkboxes is checked. However, checking any and all of them fails to change the button's disabled state.

How do I cause the command to check the CanExecute portion?

Thanks
Posted
Updated 13-Nov-14 11:58am
v3

1 solution

Thanks Stephan. I studied your solution for a while, and it looks like it may work. However, I have a dozen or so DelegateCommands sprinkled through the solution in other view models and admit that I'm reluctant to either insert a single RelayCommand into the solution or convert all the DelegateCommands to RelayCommands.

For the time being I have forced call to CanExecute and it will do until I have time to figure this out and really understand how to do it correctly.

You clearly spent a good deal of time in your response, and I can't thank you enough for that. That's really generous
 
Share this answer
 
Comments
TheRealSteveJudge 27-Nov-14 10:26am    
Hi Duke,
your're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900