wpf 如何合并DataTrigger和EventTrigger?

y1aodyip  于 2022-11-18  发布在  其他
关注(0)|答案(4)|浏览(171)

我已提出相关问题(并给出可接受的答案):How to combine DataTrigger and Trigger?

我想我需要合并EventTriggerDataTrigger来实现我所追求的目标:

  • 当某个项目出现在ListBox中时,它应该闪烁几分钟
  • 如果项目为“关键”,则应保持突出显示

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

<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之间反复动画化Highlight边框的不透明度,以吸引用户的注意力。DataTrigger决定动画化的次数。如果视图模型报告项IsCritical,则动画发生5.5次(以使其在不透明度1处结束),否则将发生5次(在不透明度0处结束)。
但是,上面的XAML不起作用,因为DataTrigger的setter失败,并显示:
在VisualTree中找不到名为“LoadedAnimation”的子级。
很公平。那么,不好意思使用自定义值转换器或者把动画计数放在视图模型上并绑定到它,我有什么选择呢?

vxf3dgd4

vxf3dgd41#

在这种情况下,我会使用行为而不是触发器。您可以编写一个行为,将事件处理程序附加到关联对象的load事件,然后应用动画。该行为可能会公开一些属性,我会公开一个AnimationCount(int)属性,告知行为要在与其相关的元素上重复动画的次数。然后,可以将此属性绑定到视图模型中的IsCritical属性,并使用值转换器将false转换为5,将true转换为5.5
希望这对你有帮助

ih99xse1

ih99xse12#

如果您可以访问Blend SDK(如果您使用的是VS2012+,则应该可以访问),您应该能够完全在XAML中完成此操作,类似于以下内容(免责声明:未测试):

<Grid HorizontalAlignment="Stretch">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="NotificationStates">
            <VisualState x:Name="Flashing">
                <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>
            </VisualState>
            <VisualState x:Name="Normal" />
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <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 -->
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <ic:GoToStateAction StateName="Flashing"/>
        </i:EventTrigger>
        <ie:DataTrigger Binding="{Binding Path=IsCritical}" Value="True">
            <ic:GoToStateAction StateName="Flashing"/>
        </ie:DataTrigger>
    </i:Interaction.Triggers>
</Grid>

将情节提要提取到VisualState,然后使用表达式库在XAML中切换状态。您将需要Microsoft.Expression.Interactions库,另请参阅WPF/Silverlight States - Activate from XAML?

eufgjt7s

eufgjt7s3#

我知道你说你不喜欢转换器的想法,但是看起来Blend解决方案需要安装一个库。转换器并不需要太多的工作,并且表明速率直接依赖于IsCritical属性:

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();
    }
}

然后更新动画:

<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

lstz6jyr

lstz6jyr4#

请尝试以下操作:

<Style x:Key="EventTriggerStyleKey">
  <Style.Triggers>
    <EventTrigger RoutedEvent="some event here">
      <!-- your animation here -->
    </EventTrigger>
  <Style.Triggers>
</Style>

<Style x:Key="myStyleKey">
  <Style.Triggers>
    <DataTrigger Binding="....." Value="......">
      <Setter Property="........." Value="......."/>
      <Setter Property="Style" Value="{StaticResource EventTriggerStyleKey}"/>
    </DataTrigger>
  <Style.Triggers>
</Style>

相关问题