• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Catch Events from Within Templates
Ask a questionAsk a question
Search Forums:
  • Search Windows Presentation Foundation (WPF) Forum Search Windows Presentation Foundation (WPF) Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

QuestionCatch Events from Within Templates

  • Monday, August 13, 2007 4:20 PMSven Hecht Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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



    • ReplyReply
    • QuoteQuote
     

All Replies

  • Wednesday, August 15, 2007 6:55 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Hello, you should use RoutedEvent="local:MyPage.NextDoc", not NextDocEvent. Just like you normally use RoutedEvent="Button.Click", not ClickEvent.

     

    • ReplyReply
    • QuoteQuote
     
  • Wednesday, August 15, 2007 4:36 PMSven Hecht Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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?
    • ReplyReply
    • QuoteQuote
     
  • Thursday, August 16, 2007 3:31 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    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.

     

     

    • ReplyReply
    • QuoteQuote
     
  • Thursday, August 16, 2007 7:12 AMSven Hecht Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Could it be that I cant catch the event because my EventTrigger is in a Template?
    • ReplyReply
    • QuoteQuote
     
  • Thursday, August 16, 2007 9:24 PMjturpin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

     

    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?

    • ReplyReply
    • QuoteQuote
     
  • Tuesday, August 21, 2007 4:52 PMRob RelyeaModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    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

    • ReplyReply
    • QuoteQuote
     
  • Wednesday, August 22, 2007 8:39 AMSven Hecht Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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.
    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement