2

I have a event trigger. I want it to be enabled only if condition occures for example only if Viewmodel.IsEnabled propery is true AND EventTrigger RoutedEvent="Window.Loaded" occured

my problem is that MultiDataTrigger and MultiTriggers cannot combine Event trigger with Data Trigger.

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

In other words I have a trigger to load a story board when window is loaded.

I want to be able to enable/disable this trigger per item.

Il Vic
  • 5,576
  • 4
  • 26
  • 37
Nahum
  • 6,959
  • 12
  • 48
  • 69

1 Answers1

1

You can use Blend Interactivity for WPF for accomplishing you task. I do not know your whole DataTemplate, so in my sample I will use an invented one.

Let's suppose I have a collection of Person objects and I want to start the DoubleAnimation just per Person whose property IsEnabled is true. I bind my collection to an ItemsControl and I create a "conditional" DataTemplate:

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

Of course you have to declare those namespaces in your XAML:

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

The ConditionBehavior object evaluates the ComparisonCondition: if the former is true, it allows the ControlStoryboardAction to run.

I hope this small sample can give you an hint for solving your issue.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Il Vic
  • 5,576
  • 4
  • 26
  • 37