views:

252

answers:

1

General context : MVVM application.

I have a View called JobView. Its DataContext is a class called Job. Within Job is a property called AuthorizationNeeded.

A Border in the view has a style (from a resource dictionary) and that style has a data trigger which starts and stops a storyboard based on the bound property AuthorizationNeeded.

<Style x:Key="AuthorizationNeededBorder"
       TargetType="Border">
    <Setter Property="Background"
            Value="Yellow" />
    <Setter Property="Opacity"
            Value="0" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding AuthorizationNeeded, FallbackValue=False}"
                     Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard Name="Flash"
                                 Storyboard="{StaticResource OneSecondOpacityFlash}" />
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="Flash" />
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

Everything works as expected. Changing AuthorizationNeeded's value starts the storyboard flash when moving to true and removes (and stops) the storyboard when moving to false.

However, if I change the DataContext of JobView to a different ViewModel (a different Job) while the storyboard is running, even if the value of AuthorizationNeeded is false in the new Job, the storyboard continues to run.

The data trigger is not seeing the change of value from AuthorizationNeeded true -> false during the DataContext change.

Any ideas on how I can get to the desired behavior of AuthorizationNeed = true = storboard running to AuthorizationNeeded = false = storyboard not running under all circumstances would be greatly appreciated. (I would prefer not to manually change the value of AuthorizationNeeded at a DataContext change because in reality there are many such triggers on this view...)

A: 

I would consider adding a trigger to the DataContextChanged event on the object. Something like:

<Style.Triggers>
    <EventTrigger EventName="DataContextChanged">
        <StopStoryboard Storyboard="{StaticResource OneSecondOpacityFlash}" />
    </EventTrigger>
</Style.Triggers>

I would wonder, though, if you want to change the DataContext on an existing view object or if it would be better to create a new view bound to the new DataContext. Depending on what you're doing, I would think that swapping out DataContexts could result in extra handles being held. Depending on what your container is, it may be easier to remove and re-create the child view/viewmodel than to swap.

Ben Von Handorf
Ben,Thank you very much for your response...and I apologize for my slow response to you. Other aspects of the project took over for a while there.I could not, unfortunately, get the DataContextChanged event to work but did take your advice on the v/vm structure. Although I was being careful to release handles (and tracking them to confirm) it was not a good mode of operation. I have therefore, based on your advice, changed the structure to release v/vms and create clean copies when needed.Again - many thanks for taking the time to answer.
Huw
Huw, Glad I was able to be of assistance! As you said, even being careful with structures and such, you basically wind up building a reference counting implementation, which adds a lot of overhead to the system.Good luck!
Ben Von Handorf