MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > Catch Events from Within Templates
發問發問
 

問題Catch Events from Within Templates

  • 2007年8月13日 下午 04:20Sven Hecht 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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



所有回覆

  • 2007年8月15日 上午 06:55Yi-Lun LuoMSFT, 版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hello, you should use RoutedEvent="local:MyPage.NextDoc", not NextDocEvent. Just like you normally use RoutedEvent="Button.Click", not ClickEvent.

     

  • 2007年8月15日 下午 04:36Sven Hecht 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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?
  • 2007年8月16日 上午 03:31Yi-Lun LuoMSFT, 版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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.

     

     

  • 2007年8月16日 上午 07:12Sven Hecht 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Could it be that I cant catch the event because my EventTrigger is in a Template?
  • 2007年8月16日 下午 09:24jturpin 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

     

    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?

  • 2007年8月21日 下午 04:52Rob Relyea版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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

  • 2007年8月22日 上午 08:39Sven Hecht 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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.