多个数据触发器 - 故事板覆盖

时间:2011-12-14 22:06:03

标签: c# wpf silverlight storyboard datatrigger

我有两个绑定到属性(Side)的DataTriggers,但只能启动一个故事板(最后一个DataTriggers中的那个)。

为什么?

我觉得最后一个故事板覆盖了第一个故事板

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

2 个答案:

答案 0 :(得分:8)

我建议在另一个故事板运行之前停止你的故事板:

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

答案 1 :(得分:1)

尝试以下解决方案,将第二个数据触发器输入第一个数据触发器退出操作

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

希望这会有所帮助