Guard method only evaluated once within a DataTemplate

Topics: Actions & Coroutines
Jun 26, 2013 at 1:35 PM
I have a ContentControl which uses DataTemplates to display it's content. Such a DataTemplate looks like this:
<DataTemplate DataType="{x:Type testCase:Foo}">
    <Border Background="CornflowerBlue">
        <StackPanel>
            <TextBlock Text="{Binding}" />
            <Button cal:Action.TargetWithoutContext="HitMeCommand" 
                    cal:Message.Attach="Execute($dataContext)">
                   Hit me!
             </Button>
        </StackPanel>
    </Border>
</DataTemplate>
When I hit the button, Execute(...) is called with the correct DataContext, but the problem is, that the CanExecute(...) guard method only gets evaluated once. If I change the Content of the ContentControl to another Foo instance, then CanExecute(...) is not evaluated again.

A tiny sample project is available here:

https://www.dropbox.com/s/6m29d2o8osqe4up/TestCase.zip
Jun 26, 2013 at 2:04 PM
An action guard declared as a function is only re-evaluated if their parameters change.
In your case I fear that the action parameter, initialized with the special value, is not updated.
Try to avoid the short syntax, and use the full one, defining the action parameter using a binding.
Jun 27, 2013 at 10:20 AM
Thanks! This works just fine:
<Button cal:Action.TargetWithoutContext="HitMeCommand">
    Hit me!
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <cal:ActionMessage MethodName="Execute">
                <cal:Parameter Value="{Binding}" />
            </cal:ActionMessage>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>