5

I'm trying to accomplish something like this:

<DataTemplate.Triggers>
    <EventTrigger RoutedEvent="{Binding MyEvent}">
        <BeginStoryboard Storyboard="{StaticResource MyAnimation}" />
    </EventTrigger>
</DataTemplate.Triggers>

MyEvent is event from my DataContext.

This does not work because RoutedEvent can't be Binding expression. Any idea how to accomplish this? In fact, I need some mix of EventTrigger and DataTrigger...

Solution with Blend SDK:

<Interactivity:Interaction.Triggers>
    <Interactivity:EventTrigger SourceObject="{Binding}" EventName="MyEvent">
        <ei:ControlStoryboardAction ControlStoryboardOption="Play">
            <ei:ControlStoryboardAction.Storyboard>
                <Storyboard>
                    ....
                </Storyboard>
            </ei:ControlStoryboardAction.Storyboard>
        </ei:ControlStoryboardAction>
    </Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
bkovacic
  • 371
  • 1
  • 5
  • 16

1 Answers1

4

Using the EventTriggers from Interactivity (Blend SDK) can trigger on any event on any object, the native ones only work for RoutedEvents which you normally only have on controls.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • ok, but how to set it to bind to event from DataContext? I've been trying a lot and can't make it work... – bkovacic Jan 09 '12 at 17:48
  • @pedala: Set the `SourceObject` to `{Binding}`, the `EventName` to the name of the event. – H.B. Jan 09 '12 at 17:54
  • I've been trying that and it didnt work. Now i tried it directly on control and it worked. Probably there's problem because it's inside `DataTemplate`? Where exactly should I put `Interactivity` inside this `DataTemplate`? I've put it inside `Grid` element because I can't put it in the root of `DataTemplate` as I've put `Trigger` in the question.. – bkovacic Jan 09 '12 at 18:03
  • @pedala: Use the root element of the `DataTemplate` as it needs an object to attach to. – H.B. Jan 09 '12 at 18:05
  • I was using `SourceName` instead of `SourceObject`. This part is working now. But, when I raise this event, I get exception: `No applicable name scope exists to resolve the name 'MyGrid'.` Any idea what that might be? – bkovacic Jan 09 '12 at 19:09
  • I solved this exception. I've put `Storyboard` inside `ControlStoryboardAction` instead of using it as a StaticResource. Thank you very much! – bkovacic Jan 09 '12 at 19:28