Question

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...)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top