C# 事件触发条件为on

C# 事件触发条件为on,c#,wpf,C#,Wpf,我有一个事件触发器。 我希望仅当条件发生时才启用它 例如,仅当Viewmodel.IsEnabled属性为true且发生EventTrigger RouteEvent=“Window.Loaded”时 我的问题是MultiDataTrigger和MultiTrigger不能将事件触发器与数据触发器结合起来 <DataTemplate.Triggers> <EventTrigger RoutedEvent="Window.Loaded" SourceName=

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

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

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

换句话说,当窗口加载时,我有一个触发器来加载故事板

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

您可以使用它来完成任务。我不知道您的整个数据模板,所以在我的示例中,我将使用一个虚构的数据模板

假设我有一个
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,则允许运行
ControlStoryboard操作

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