如何组合DataTrigger和EventTrigger?

时间:2009-11-05 09:35:39

标签: .net wpf xaml triggers datatrigger

  

注意我已经提出相关问题(已接受答案):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动画Highlight边框的不透明度在0和1之间,并在首次加载项目时反复返回,引起用户注意它。 DataTrigger确定动画的次数。如果视图模型报告项目IsCritical然后动画发生5.5次(这样它以不透明度1结束),否则它会发生5次(以不透明度0结束。)

但是上面的XAML不起作用,因为DataTrigger的setter失败了:

  

在VisualTree中找不到名称为“LoadedAnimation”的子项。

足够公平。因此,不喜欢使用自定义值转换器或将动画计数放在视图模型上并绑定到它,我有什么选择?

4 个答案:

答案 0 :(得分:0)

如果您可以访问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>

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

答案 1 :(得分:0)

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

希望这有帮助

答案 2 :(得分:0)

我知道你说你并不热衷于转换器的想法,但看起来Blend解决方案需要安装一个库。转换器的工作量很大,并且表明该速率直接取决于Beginning deployment... ERROR: Error Response: [403] Request had insufficient authentication scopes. ERROR: (gcloud.preview.app.deploy) Could not retrieve the default Google Cloud Storage bucket for [test]. Please try again or use the [bucket] argument. 属性:

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

答案 3 :(得分:-4)

尝试这样的事情:

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