WPF: how to send RoutedEvent to be triggered inside a DataTemplate ?
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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));
} -
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));
}