Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF: how to send RoutedEvent to be triggered inside a DataTemplate ?

WPF: how to send RoutedEvent to be triggered inside a DataTemplate ?

Scheduled Pinned Locked Moved WPF
wpfcsharpcssdatabasewcf
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    christoph brandle
    wrote on last edited by
    #1

    Many people have seen the flipping panel by Ian Griffiths as shown by Sacha Barber on this MyFriends sample on CP. http://www.interact-sw.co.uk/iangblog/2007/05/17/wpf-flippable-3D-list http://www.codeproject.com/KB/WPF/MyFriends.aspx I try now to figure out, how to invoke the trigger to flip the page by a RoutedEvent, but I cannot get it there.

        public void ShowBack()
        {
            DataTemplate \_dt = (DataTemplate)this.FindResource("flipItemTemplate");
            RaiseEvent(new RoutedEventArgs(FlipToBackEvent, \_dt.VisualTree));
        }
    

    should call this one:

            <DataTemplate x:Key="flipItemTemplate">
                <Grid Width="270" Height="270">
                    <Viewport3D Grid.Column="0" x:Name="vp3D" Visibility="Hidden">
                    </Viewport3D>
    
                    <Border x:Name="frontWrapper">
                        <Border x:Name="frontHost" Background="Transparent">
                            <Border.Triggers>
                                <EventTrigger RoutedEvent="local:UserControl1.FlipToBackEvent">
                                    <BeginStoryboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Border.Triggers>
                            <ContentPresenter  Content="{Binding}" ContentTemplate="{StaticResource frontTemplate}" />
                        </Border>
                    </Border>}
    

    Does anyone know how I can invoke from Codebehind the Storyboard inside the Trigger, inside the DataTemplate? Tips on this are welcome :) Christoph

    S 1 Reply Last reply
    0
    • C christoph brandle

      Many people have seen the flipping panel by Ian Griffiths as shown by Sacha Barber on this MyFriends sample on CP. http://www.interact-sw.co.uk/iangblog/2007/05/17/wpf-flippable-3D-list http://www.codeproject.com/KB/WPF/MyFriends.aspx I try now to figure out, how to invoke the trigger to flip the page by a RoutedEvent, but I cannot get it there.

          public void ShowBack()
          {
              DataTemplate \_dt = (DataTemplate)this.FindResource("flipItemTemplate");
              RaiseEvent(new RoutedEventArgs(FlipToBackEvent, \_dt.VisualTree));
          }
      

      should call this one:

              <DataTemplate x:Key="flipItemTemplate">
                  <Grid Width="270" Height="270">
                      <Viewport3D Grid.Column="0" x:Name="vp3D" Visibility="Hidden">
                      </Viewport3D>
      
                      <Border x:Name="frontWrapper">
                          <Border x:Name="frontHost" Background="Transparent">
                              <Border.Triggers>
                                  <EventTrigger RoutedEvent="local:UserControl1.FlipToBackEvent">
                                      <BeginStoryboard>
                                      </BeginStoryboard>
                                  </EventTrigger>
                              </Border.Triggers>
                              <ContentPresenter  Content="{Binding}" ContentTemplate="{StaticResource frontTemplate}" />
                          </Border>
                      </Border>}
      

      Does anyone know how I can invoke from Codebehind the Storyboard inside the Trigger, inside the DataTemplate? Tips on this are welcome :) Christoph

      S Offline
      S Offline
      Sacha Barber
      wrote on last edited by
      #2

      Christoph Not sure what you are trying to do with the VisualTree as part of the FlipToBackEvent, but I would have thought you don't need to do that, and just raise the FlipToBackEvent event when you need to and them XAML Trigger will see that, and act accordingly. This would seem right to me, but I just don't have the time right now to look further, sorry.

      Sacha Barber

      • Microsoft Visual C# MVP 2008
      • Codeproject MVP 2008

      Your best friend is you. I'm my best friend too. We share the same views, and hardly ever argue My Blog : sachabarber.net

      C 1 Reply Last reply
      0
      • S Sacha Barber

        Christoph Not sure what you are trying to do with the VisualTree as part of the FlipToBackEvent, but I would have thought you don't need to do that, and just raise the FlipToBackEvent event when you need to and them XAML Trigger will see that, and act accordingly. This would seem right to me, but I just don't have the time right now to look further, sorry.

        Sacha Barber

        • Microsoft Visual C# MVP 2008
        • Codeproject MVP 2008

        Your best friend is you. I'm my best friend too. We share the same views, and hardly ever argue My Blog : sachabarber.net

        C Offline
        C Offline
        christoph brandle
        wrote on last edited by
        #3

        Sacha Just fire was my first attempt, and works for Triggers that are not inside a DataTemplate, so I tried different things.. they all don't work :( Thanks anyway

        S 1 Reply Last reply
        0
        • C christoph brandle

          Sacha Just fire was my first attempt, and works for Triggers that are not inside a DataTemplate, so I tried different things.. they all don't work :( Thanks anyway

          S Offline
          S Offline
          Sacha Barber
          wrote on last edited by
          #4

          Ah ok, sorry to hear that. It sounded like that would work. Sorry I don't have more time to look at this myself.

          Sacha Barber

          • Microsoft Visual C# MVP 2008
          • Codeproject MVP 2008

          Your best friend is you. I'm my best friend too. We share the same views, and hardly ever argue My Blog : sachabarber.net

          C 1 Reply Last reply
          0
          • S Sacha Barber

            Ah ok, sorry to hear that. It sounded like that would work. Sorry I don't have more time to look at this myself.

            Sacha Barber

            • Microsoft Visual C# MVP 2008
            • Codeproject MVP 2008

            Your best friend is you. I'm my best friend too. We share the same views, and hardly ever argue My Blog : sachabarber.net

            C Offline
            C Offline
            christoph brandle
            wrote on last edited by
            #5

            Sacha - found it finally (I rarely give up) this one was hard for me, then I came to this // http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4a04938b-690b-4bd0-969a-6dc4f6d6fda5/ this.displayContent = ContentControl

                    foreach (Border \_border in FindInVisualTree(this.displayContent, a => { return a is Border; }))
                    {
                        \_border.RaiseEvent(new RoutedEventArgs(FlipToLoginEvent));
                    }
            

            - Christoph

            S 1 Reply Last reply
            0
            • C christoph brandle

              Sacha - found it finally (I rarely give up) this one was hard for me, then I came to this // http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4a04938b-690b-4bd0-969a-6dc4f6d6fda5/ this.displayContent = ContentControl

                      foreach (Border \_border in FindInVisualTree(this.displayContent, a => { return a is Border; }))
                      {
                          \_border.RaiseEvent(new RoutedEventArgs(FlipToLoginEvent));
                      }
              

              - Christoph

              S Offline
              S Offline
              Sacha Barber
              wrote on last edited by
              #6

              Cool, glad you are now sorted. I would have maybe gone for the x:Key idea on that thread.

              Sacha Barber

              • Microsoft Visual C# MVP 2008
              • Codeproject MVP 2008

              Your best friend is you. I'm my best friend too. We share the same views, and hardly ever argue My Blog : sachabarber.net

              C 1 Reply Last reply
              0
              • S Sacha Barber

                Cool, glad you are now sorted. I would have maybe gone for the x:Key idea on that thread.

                Sacha Barber

                • Microsoft Visual C# MVP 2008
                • Codeproject MVP 2008

                Your best friend is you. I'm my best friend too. We share the same views, and hardly ever argue My Blog : sachabarber.net

                C Offline
                C Offline
                christoph brandle
                wrote on last edited by
                #7

                you are right, not perfect though working here I present my final solution (for the ones that search the internet desperately :)

                    //  http://www.beacosta.com/blog/?p=41
                    private T GetObjectOfTypeInVisualTree<T>(DependencyObject dpob) where T : DependencyObject
                    {
                        int count = VisualTreeHelper.GetChildrenCount(dpob);
                        for (int i = 0; i < count; i++)
                        {
                            DependencyObject child = VisualTreeHelper.GetChild(dpob, i);
                            T childAsT = child as T;
                            if (childAsT != null)
                            {
                                return childAsT;
                            }
                            childAsT = GetObjectOfTypeInVisualTree<T>(child);
                            if (childAsT != null)
                            {
                                return childAsT;
                            }
                        }
                        return null;
                    }
                
                    private void ManualStartAnimation(string templateName, string elementName, RoutedEventArgs eventArgs)
                    {
                        //  this works too
                        //  http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4a04938b-690b-4bd0-969a-6dc4f6d6fda5/
                
                        //  this is implemented
                        //  http://joshsmithonwpf.wordpress.com/2007/06/28/how-to-use-findname-with-a-contentcontrol/
                        //  together with this (GetObjectOfTypeInVisualTree)
                        //  http://www.beacosta.com/blog/?p=41
                
                        DataTemplate   \_dt = (DataTemplate)this.FindResource(templateName);
                        if (\_dt != null)
                        {
                           //  reminder: displayContent is the ContentControl
                            ContentPresenter \_cp = GetObjectOfTypeInVisualTree<ContentPresenter>(this.displayContent);
                            if (\_cp != null)
                            {
                                UIElement \_element = (UIElement)\_dt.FindName(elementName, \_cp);
                                if (\_element != null)
                                {
                                    \_element.RaiseEvent(eventArgs);
                                }
                            }
                        }
                    }
                
                    public void ShowLogin()
                    {
                

                ->kick ManualStartAnimation("flipItemTemplate", "frontHost", new RoutedEventArgs(FlipToLoginEvent));
                }

                J 1 Reply Last reply
                0
                • C christoph brandle

                  you are right, not perfect though working here I present my final solution (for the ones that search the internet desperately :)

                      //  http://www.beacosta.com/blog/?p=41
                      private T GetObjectOfTypeInVisualTree<T>(DependencyObject dpob) where T : DependencyObject
                      {
                          int count = VisualTreeHelper.GetChildrenCount(dpob);
                          for (int i = 0; i < count; i++)
                          {
                              DependencyObject child = VisualTreeHelper.GetChild(dpob, i);
                              T childAsT = child as T;
                              if (childAsT != null)
                              {
                                  return childAsT;
                              }
                              childAsT = GetObjectOfTypeInVisualTree<T>(child);
                              if (childAsT != null)
                              {
                                  return childAsT;
                              }
                          }
                          return null;
                      }
                  
                      private void ManualStartAnimation(string templateName, string elementName, RoutedEventArgs eventArgs)
                      {
                          //  this works too
                          //  http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4a04938b-690b-4bd0-969a-6dc4f6d6fda5/
                  
                          //  this is implemented
                          //  http://joshsmithonwpf.wordpress.com/2007/06/28/how-to-use-findname-with-a-contentcontrol/
                          //  together with this (GetObjectOfTypeInVisualTree)
                          //  http://www.beacosta.com/blog/?p=41
                  
                          DataTemplate   \_dt = (DataTemplate)this.FindResource(templateName);
                          if (\_dt != null)
                          {
                             //  reminder: displayContent is the ContentControl
                              ContentPresenter \_cp = GetObjectOfTypeInVisualTree<ContentPresenter>(this.displayContent);
                              if (\_cp != null)
                              {
                                  UIElement \_element = (UIElement)\_dt.FindName(elementName, \_cp);
                                  if (\_element != null)
                                  {
                                      \_element.RaiseEvent(eventArgs);
                                  }
                              }
                          }
                      }
                  
                      public void ShowLogin()
                      {
                  

                  ->kick ManualStartAnimation("flipItemTemplate", "frontHost", new RoutedEventArgs(FlipToLoginEvent));
                  }

                  J Offline
                  J Offline
                  jp chow
                  wrote on last edited by
                  #8

                  THANK YOU! I so did desperately search the internet and I found your nice solution. Thanks again! :laugh:

                  modified on Friday, January 9, 2009 3:19 PM

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups