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 Snippet

public 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



[2552 byte] By [SvenHecht] at [2008-1-8]
# 1
Hello, you should use RoutedEvent="local:MyPage.NextDoc", not NextDocEvent. Just like you normally use RoutedEvent="Button.Click", not ClickEvent.
Yi-LunLuo-MSFT at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2
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?
SvenHecht at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

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.

Yi-LunLuo-MSFT at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4
Could it be that I cant catch the event because my EventTrigger is in a Template?
SvenHecht at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

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?

jturpin at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6

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

RobRelyea at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 7
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.
SvenHecht at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified