如何结合DataTrigger和EventTrigger中?DataTrigger、EventTrigger

2023-09-03 15:53:37 作者:汐﹌

注意我已要求相关的问题(与所接受的答案):的如何结合DataTrigger和触发?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger?

我想我需要结合一个的EventTrigger DataTrigger 来实现我以后是:

I think I need to combine an EventTrigger and a DataTrigger to achieve what I'm after:

当一个项目出现在我的列表框,它应闪烁片刻 如果该项目是严重,那么它​​应该继续强调

目前我有一个DataTemplate,看起来像这样:

Currently I have a DataTemplate that looks like this:

<DataTemplate DataType="{x:Type Notifications:NotificationViewModel}">
    <Grid HorizontalAlignment="Stretch">
        <Border Name="Background" CornerRadius="8" Background="#80c0c0c0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <Border Name="Highlight"  CornerRadius="8" Background="Red"       HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <!-- snip actual visual stuff -->
        <Grid.Triggers>
            <EventTrigger RoutedEvent="Grid.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation x:Name="LoadedAnimation" 
                                             Storyboard.TargetName="Highlight" 
                                             Storyboard.TargetProperty="Opacity" 
                                             From="0" To="1" 
                                             RepeatBehavior="5x" 
                                             Duration="0:00:0.2" 
                                             AutoReverse="True" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Grid.Triggers>
    </Grid>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=IsCritical}" Value="True">
            <Setter TargetName="LoadedAnimation" Property="RepeatBehavior" Value="5.5x" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

这个想法是一个EventTrigger动画的突出 0和1之间,然后再返回该项目是第一次加载时重复边界的不透明度,提请用户注意它。该 DataTrigger 确定的时间以动画的数量。如果视图模型报告, IsCritical 的项目,然后动画时的5.5倍(例如,它在不透明1结束),否则会出现5次(截止于不透明0)

The idea is that an EventTrigger animates the Highlight border's opacity between 0 and 1 and back again repeatedly when the item is first loaded, drawing the user's attention to it. The DataTrigger determines the number of times to animate. If the view model reports that the item IsCritical then the animation occurs 5.5 times (such that it ends at opacity 1), otherwise it occurs 5 times (ending at opacity 0.)

不过上述XAML不起作用,因为该DataTrigger的制定者与失败:

However the above XAML doesn't work because the DataTrigger's setter fails with:

儿童与名称LoadedAnimation在VisualTree没有找到。

Child with Name 'LoadedAnimation' not found in VisualTree.

不够公平。所以,害羞使用自定义值转换器或将动画指望视图模型,并结合它,我有哪些选择?

Fair enough. So, shy of using a custom value converter or putting the animation count on the view model and binding to it, what are my options?

推荐答案

我知道你说你是不是热衷于转换器的想法,但它看起来像混合解决方案,需要安装一个图书馆。该转换器是没有太大的工作和信号的意图,房价是直接依赖于 IsCritical 属性:

I know you said you weren't keen on the idea of a converter, but it looks like the Blend solutions require a library to be installed. The converter isn't much work and signals intent that the rate is directly dependent on the IsCritical property:

public class CriticalAnimationRateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Error handling omitted for brevity.
        if ((bool)value)
            return new System.Windows.Media.Animation.RepeatBehavior(5.5);
        else
            return new System.Windows.Media.Animation.RepeatBehavior(5.0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后更新您的动画:

And then update your animation:

<DoubleAnimation Storyboard.TargetName="Highlight"
                 Storyboard.TargetProperty="Opacity"
                 From="0"
                 To="1"
                 RepeatBehavior="{Binding IsCritical, Converter={StaticResource CriticalAnimationRateConverter}}"
                 Duration="0:00:0.2"
                 AutoReverse="True" />

DataTrigger 然后删除。