Prism4 MVVM DelegateCommand dose not refresh

Topics: Prism v4 - Silverlight 4
Nov 7, 2010 at 1:50 PM

Hi,

I am not sure if its the Prism issue, but as I am using prism i'll ask here any ways:

I have a button on my Usercontrol that has a command binding t o my View Model SaveCommand and I use the DelegateCommand with both the SaveChanges and CanSaveChanges.

<Button Margin="3" Style="{StaticResource ToolButtonStyle}" IsTabStop="False" Command="{Binding SaveCommand}">

 In my ViewModel I have the command setup as below

SaveCommand =new DelegateCommand(SaveAllChanges, CanSaveChanges);

It so happens the CanSaveChanges is called only once at the startup and when some change is done on the DomainContext I would like the button to revalidate it but don't know a way to handle this in a descent way.

To work around the issue, I have hooked in to the context.PropertyChanged and have to reassign the SaveCommand which allows the button to revalidate, but I was wondering if there was any simplear and better way to handle this?

Regards, Kiran

Nov 8, 2010 at 2:56 PM
Edited Nov 8, 2010 at 3:00 PM

Hi Kiran,

If you click a button that has a Command assigned, you application should call to the CanExecute method. I created a simple sample for this, so you could download this from here and use it as a reference.

You could use a breakpoint in the CanExecute method or take a look at the output window.

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Nov 8, 2010 at 4:10 PM

Kiran,

You just need to call

SaveCommand.RaiseCanExecuteChanged()

at the appropriate time - e.g. when the save command should be enabled or disabled.  That's up to you to decide based on the state of your view model.

Phil

Nov 8, 2010 at 7:04 PM

Phil,

Thanks, this is what I was missing. I misses it as my SaveCommand was declared as ICommand.

Fernando Antivero,

Thanks for the sample, but the issue was that I had to control the CanExecute from outside the button click like when DomainDatasource gets dirty etc. I know exactly when the data is dirty in my ViewModel, but was unable to locate the way to ask the View to take action. but now with the "RaiseCanExecuteChanged" it works.

Thanks & Regards, Kiran