Catch Events from Within Templates
- Here the pretty much simplyfied code:
Code Snippet<Page x:Name="myPageObj">
<Page.Resources><DataTemplate DataType="{x:Type local:ImageMenuData}"><Border><Border.Triggers><EventTrigger SourceName="myPageObj" RoutedEvent="MyPage.NextDocEvent">
<BeginStoryboard Storyboard="{StaticResource 3dFlipAnimation}"/></EventTrigger><Border.Triggers></Border></DataTemplate></Page.Resources>
...</Page>
Now I want to throw the NextDocEvent from within c# code.
The definition of the Event:
Code Snippetpublic static readonly RoutedEvent NextDocEvent = EventManager.RegisterRoutedEvent( "NextDoc", RoutingStrategy.Tunnel, typeof( RoutedEventHandler ), typeof( MyPage ) );
public event RoutedEventHandler NextDoc
{add { AddHandler( NextDocEvent, value ); }remove { RemoveHandler( NextDocEvent, value ); }}
void RaiseNextDocEvent()
{RoutedEventArgs newEventArgs = new RoutedEventArgs( MyPage.NextDocEvent );
RaiseEvent(newEventArgs);}
this approach seems not to work because I get this Error:
Code Snippet'MyPage.NextDocEvent' value cannot be assigned to property 'RoutedEvent' of object 'System.Windows.EventTrigger'. Value cannot be null.
Parameter name: value Error at object 'System.Windows.EventTrigger' in markup file
All Replies
- Hello, you should use RoutedEvent="local:MyPage.NextDoc", not NextDocEvent. Just like you normally use RoutedEvent="Button.Click", not ClickEvent.
- stupid me! Of course.
Now it compiles but still the event is not catched.
if I call the Raise-Method the EventTrigger doesnt get the tunneled event it should shouldn't it? This works fine for me:
In the Window’s code behind, create a routed event:
public static readonly RoutedEvent TestEvent = EventManager.RegisterRoutedEvent("Test", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(Window1));
public event RoutedEventHandler Test
{
add { AddHandler(TestEvent, value); }
remove { RemoveHandler(TestEvent, value); }
}
private void RaiseTestEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(Window1.TestEvent);
RaiseEvent(newEventArgs);
}
Raise this event when clicking a Button (are you missing this step?):
private void Button1_Click(object sender, RoutedEventArgs e)
{
RaiseTestEvent();
}
Use an EventTrigger to handle this event:
<EventTrigger RoutedEvent="local:Window1.Test">
<BeginStoryboard Storyboard="{StaticResource sb}"/>
</EventTrigger>
The animation plays successfully.
- Could it be that I cant catch the event because my EventTrigger is in a Template?
I have a very similar situation. My post here explains this. Have you figured out what your issue is? I have been struggling with this for a while now - need to find some alternate solutions soon if I can't figure out what is going on.
I have double-checked and tripple checked for syntatic/spelling errors which the compiler might not catch and have found nothing. For some reason my DataTemplate is not catching the event even though I have verified that the ListBoxItem it is applied to can from code.
Any suggestions out there on how to debug this?
DataTemplate is a NameScope (implements INameScope), so the SourceName in the EventTrigger is looking for myPageObj in the DataTemlate, not in the Page.
Thanks, Rob
Rob Relyea | Program Manager, WPF & Xaml Language Team
robrelyea.com | /blog | /wpf | /xaml- Thanks Rob that was a slightly more helpful piece of information.
So i am in another scope.
Is there a Chance to Trigger that EvenTrigger from within other scopes?
Any solution is ok even a hacky one. I need to catch that Event in the templatescope else I have to work around and remove almost all my templates and work them into my definitions.
That would be good night DRY.

