0

我有以下 XAML 代码,它在 LastDataUpdate 字段更改时触发矩形动画。LastDataUpdate 是实现 INotifyPropertyChanged 的​​类中的 DateTime。如果 LastDataUpdate==DateTime.MinValue,我希望动画不运行。有什么方法可以在 XAML 中实现吗?

<Rectangle x:Name="NewDataAnimation" Tag="{Binding Path=LastDataUpdate, NotifyOnTargetUpdated=True}" Opacity="0" Width="5" Height="5" Fill="LawnGreen" HorizontalAlignment="Left" VerticalAlignment="Top">
<Rectangle.Style>
  <Style>
    <Style.Triggers>
      <EventTrigger RoutedEvent="Binding.TargetUpdated">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0" To="1.0" />
              <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:4" From="1.0" To="0.0" BeginTime="0:0:2" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </Style.Triggers>
  </Style>
</Rectangle.Style>

安德鲁

4

1 回答 1

0

为什么,不触发属性本身并让转换器确定是否执行动画?

下面的 XAML 假设您创建了一个转换器,它将根据您不想要的值评估 LastDataUpdate。如果将 LastDataUpdate 设置为 DateTime.MinValue,动画将不会开始。一旦您进行更改,绑定将被迫通过 PropertyChanged 重新评估,并让转换器进行决策...

<Rectangle Opacity="0" Width="5" Height="5" Fill="LawnGreen" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Rectangle.Style>
        <Style>
            <Style.Triggers>
                <Trigger Property="{Binding LastDataUpdate, Converter={StaticResource LastDataUpdateToDoAnimation}}" Value="True" >
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0" To="1.0" />
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:4" From="1.0" To="0.0" BeginTime="0:0:2" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
</Rectangle>
于 2012-04-09T23:00:05.703 回答