条件为的EventTrigger触发器

本文关键字:触发器 EventTrigger 条件 | 更新日期: 2023-09-27 18:29:31

我有一个事件触发器。我希望只有在出现条件时才启用它例如,仅当Viewmodel.IsEnabled属性为true且EventTrigger RoutedEvent="Window.Loaded"发生时

我的问题是MultiDataTrigger和MultiTriggers不能将事件触发器和数据触发器结合起来。

  <DataTemplate.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded" SourceName="NotificationWindow">
              <BeginStoryboard x:Name="FadeInStoryBoard">
                    <Storyboard>
                          <DoubleAnimation Storyboard.TargetName="NotificationWindow" From="0.01" To="1" Storyboard.TargetProperty="Opacity" Duration="0:0:2"/>
                    </Storyboard>
              </BeginStoryboard>
        </EventTrigger>
  </DataTemplate.Triggers>

换句话说,我有一个触发器,可以在加载窗口时加载故事板。

我希望能够为每个项目启用/禁用此触发器。

条件为的EventTrigger触发器

您可以使用WPF的Blend Interactivity来完成任务。我不知道你的整个DataTemplate,所以在我的样本中,我会使用一个发明的。

假设我有一个Person对象的集合,并且我想只为属性IsEnabled为true的每个人启动DoubleAnimation。我将我的集合绑定到ItemsControl;有条件的";数据模板:

<ItemsControl ItemsSource="{Binding Path=People}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border Name="Border"  BorderBrush="Gray" BorderThickness="1" CornerRadius="4" Margin="2">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Loaded">
                        <i:Interaction.Behaviors>
                            <ei:ConditionBehavior>
                                <ei:ConditionalExpression>
                                    <ei:ComparisonCondition LeftOperand="{Binding IsEnabled}" RightOperand="True"/>
                                </ei:ConditionalExpression>
                            </ei:ConditionBehavior>
                        </i:Interaction.Behaviors>
                        <ei:ControlStoryboardAction ControlStoryboardOption="Play">
                            <ei:ControlStoryboardAction.Storyboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="Border" From="0.01" To="1" Storyboard.TargetProperty="Opacity" Duration="0:0:2"/>
                                </Storyboard>
                            </ei:ControlStoryboardAction.Storyboard>
                        </ei:ControlStoryboardAction>    
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <TextBlock Text="{Binding Path=Surname, Mode=OneWay}" Margin="2" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

当然,您必须在XAML中声明这些名称空间:

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

ConditionBehavior对象评估ComparisonCondition:如果前者为true,则允许运行ControlStoryboardAction

我希望这个小样本能给你一个解决问题的提示。