This project is read-only.

CanExecuteHandler doesn't work with Button in WPF

Nov 9, 2010 at 2:49 PM


I'm trying to disable Button if the CanExecuteHandler in ActionCommand returns false. I've added the EventTrigger to Button in this way:

<Button Content="Edit">
                    <i:EventTrigger EventName="Click">
                        <n:ExecuteCommandAction Command="{Binding EditCommand}" ManageEnableState="True"/>

I would like in some conditions not to enable user to click the button Edit. I thought that setting ManageEnableState would do the trick, but it doesn't.

Could you help me and tell what am I doing wrong?

Nov 9, 2010 at 7:47 PM

Well, I'm not 100% sure how you've set it up - but let me show you an example as to how the UI's enable state can be toggled:

// property
public ActionCommand CommitCommand { get; set; }

// set it up
CommitCommand = new ActionCommand(OnCommit,
	() => !this.IsWorking && this.CurrentItem != null).
	RequeryOnPropertyChanged(this, () => IsWorking).
	RequeryOnPropertyChanged(this, () => CurrentItem);

// handler
private void OnCommit() 
	// does something

In the case above, we have a CommitCommand that should be executable if IsWorking property is not true and CurrentItem is not null - this is declaratively expressed with the lambda statement in line 5. On the UI side of the things, the button when trying to manage the state listens to the ICommand's CanExecuteChanged event to resolve if the UI control should be enabled or not. So accordingly, in our VM end we need to raise the CanExecuteChanged event on the ICommand to indicate changes in the ICommand's execut'ability - and in nRoute you have two options for this, one is to manually raise the event using the RequeryCanExecute method or like above use the RequeryXXX extension methods that do it for you per the appropriate criteria. In the case above, the extension methods attach INPC listeners that trigger the requery when the specified property changes - which in turn updates the UI.

So, if you have setup the mechanism correctly, I believe your button should toggle correctly. 
Hope this helps,


Nov 9, 2010 at 8:15 PM

Hi Rishi,

thanks for your post. Your help make a trick :)