Search code examples
c#wpfsilverlightstoryboarddatatrigger

Multiple DataTriggers - Storyboard overriden


I have two DataTriggers binded to a property (Side) but only one storyboard could be started (the one in the last DataTriggers).

Why ?

I feel like the last storyboard override the first one

<Border x:Name="layout" Background="Transparent" BorderBrush="#BAC8CE" BorderThickness="1" CornerRadius="5">
  <Border.Style>
    <Style TargetType="{x:Type Border}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Side}" Value="Up">
          <DataTrigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                                Duration="00:00:01"
                                From="Transparent"
                                To="Green"/>
                <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                                Duration="00:00:00.5" 
                                From="Green"
                                To="Transparent"/>
              </Storyboard>
            </BeginStoryboard>
          </DataTrigger.EnterActions>
        </DataTrigger>
        <DataTrigger Binding="{Binding Side}" Value="Down">
          <DataTrigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                                Duration="00:00:01" 
                                From="Transparent"
                                To="Red"/>
                <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                Duration="00:00:00.5" 
                                From="Red"
                                To="Transparent"/>
              </Storyboard>
            </BeginStoryboard>
          </DataTrigger.EnterActions>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Border.Style>
</Border>

Solution

  • I would suggest stopping your storyboard before the other one runs:

    <Border x:Name="layout" Background="Transparent" BorderBrush="#BAC8CE" BorderThickness="1" CornerRadius="5">
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Side}" Value="Up">
                        <DataTrigger.EnterActions>
                            <StopStoryboard BeginStoryboardName="BeginStoryboardTwo" />
                            <BeginStoryboard x:Name="BeginStoryboardOne">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                                                    Duration="00:00:01"
                                                    From="Transparent"
                                                    To="Green"/>
                                    <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                                                    Duration="00:00:00.5" 
                                                    From="Green"
                                                    To="Transparent"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Side}" Value="Down">
                        <DataTrigger.EnterActions>
                            <StopStoryboard BeginStoryboardName="BeginStoryboardOne" />
                            <BeginStoryboard x:Name="BeginStoryboardTwo" >
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                                                    Duration="00:00:01" 
                                                    From="Transparent"
                                                    To="Red"/>
                                    <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                                    Duration="00:00:00.5" 
                                                    From="Red"
                                                    To="Transparent"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <TextBlock Text="Hello World!" />
    </Border>