Modify data structure being passed to CanExecute event?

Ribbon for WPF Forum

Posted 12 years ago by Chris Bunker
Version: 11.2.0551
Avatar

Hi -

The documentation for the Checkable Controls state that:

"By default, the built-in checkable controls assign a CheckableCommandParameter instance (which implements ICheckableCommandParameter) to their CommandParameter property. "

 

<controls:Button Command="{x:Static gui:MyCommands.SelectWidgetCommand}"
                 CommandParameter="{Binding Path=ViewModel}"/>

 

 

 

Comments (9)

Posted 12 years ago by Chris Bunker
Avatar

Looks like the embedded code section blew away my question.  Here's what was supposed to be just before the code section:

My question is that, can I change the default object that's passed to the CanExecute function to be a structure that contains both the CheckableCommandParameter AND the CommandParameter I specified for the Execute function of the command? I'm implementing a button for a menu item, and I desire to have the checkable functionality, but in order for me to determine if the item should be checked or not, I need the command parameter that I specified in the XAML for the button.

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Chris,

Yes you can modify it but if you want the button to support checked states retrievable from the command parameter, you'd need your ViewModel to implement the ICheckableCommandParameter interface.  By doing that, you can still have your ViewModel object and support command-driven check states.


Actipro Software Support

Posted 12 years ago by Chris Bunker
Avatar

Thanks for the quick response.  Is this the only way of accomplishing what I'd like to do?  It seems that this could work, but I worry about the same view model being used by multiple commands (i.e. multiple commands reusing the same IsChecked flag). 

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

For making it command driven, yes, that is the only way.  If you are putting the same VM object in all your various command-driven buttons, then yes, that will cause a problem.

Perhaps you could have your command parameter object implement something like IViewModelProvider and have it return the VM from that.  That way you could still have something implement ICheckableCommandParameter but could also get at your core shared VM.


Actipro Software Support

Posted 12 years ago by Chris Bunker
Avatar

Thanks for the suggestion.  Before I go to any great length to do some refactoring, I thought I'd give your earlier suggestion a try.  I now have my menu item's CommandParameter property bound to an object that derives from ICheckableCommandParameter.  However, in the CanExecute function of the command, the CanExecuteRoutedEventArgs Parameter property is still just the default CheckableCommandParameter, NOT the derived command parameter I supplied in the CommandParameter binding.  I do see the derived command parameter being passed as the Parameter property in the Execute function, as I would expect.

I suppose there's code in the control's command binding code to inspect the CommandParameter object to see if the object is or derives from ICheckableCommandParameter.  If it is or derives from it, then it will use the CommandParameter as is, and if not, it will create a default CheckableCommandParameter object instead?

Thanks again for the assistance.

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Chris,

It may be the default value initially since your Binding might not take effect until after WPF commands do their thing.  I would expect that after the binding took effect, any subsequent calls would be your bound command parameter.


Actipro Software Support

Posted 12 years ago by Chris Bunker
Avatar

Sorry, I'm not sure I understand your explanation.  I would expect the binding code for the CommandParameter to execute before the call to CanExecute, otherwise, how would your suggestion work in the first place?

My CommandParameter is bound to a ICheckableCommandParameter-derived object.  In my CanExecute function, I am still receiving the default CheckableCommandParameter in the CanExecuteRoutedEventArgs.  In my Execute function, I receive the expected object that I setup the binding for. 

It appears that there's nothing I can do to change the type of the CanExecuteRoutedEventArgs Parameter being sent to the CanExecute function, even though I provide an object derived from ICheckableCommandParameter for the CommandParameter binding.

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

I am just saying that WPF command can-execute checks might be initially occurring before your binding takes effect.  I just did a test in a simple project and do believe I see that happening.  The first CanExecute call has null e.Parameter passed for the Button, then the second time it's called, the bound value comes through.

I'm not seeing the default CheckableCommandParameter set though.  If you'd like us to look into it further, then please make a new simple sample project that shows the issue and email that over.  We can debug it and see what is happening.


Actipro Software Support

Posted 12 years ago by Chris Bunker
Avatar

Just getting back to this.   I created a test program, and it appears to be working as expected.  I went back to the code in my project and it also appears to be working as expected.  I'm not sure what was happening before, but I'm happy with the way it works now.  Thanks for your support!

Chris

The latest build of this product (v24.1.3) was released 29 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.