I have a frame. I switch pages with this line:
FrameName.Content = new PageName();
I want a storyboard to begin when the page has changed, and I want to do it in XAML, and not in code-behind. I have tried the following code:
<Frame.Triggers>
<EventTrigger RoutedEvent="ContentChanged">
<BeginStoryboard Storyboard="{StaticResource storyboardName}" />
</EventTrigger>
</Frame.Triggers>
After searching a bit I realized there is no built-in routed event of that nature. The first answer here suggests that
The most dynamic approach is to simply derive your own
labelcontrol that provides a ContentChanged event.
I have tried to implement the code in this answer:
using System.Windows;
using System.Windows.Controls;
namespace ContentChangedTest
{
class MyFrame : Frame
{
public event DependencyPropertyChangedEventHandler ContentChanged;
static MyFrame()
{
ContentProperty.OverrideMetadata(typeof(MyFrame), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnContentChanged)));
}
private static void OnContentChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
MyFrame frame = dp as MyFrame;
if (frame.ContentChanged != null)
{
DependencyPropertyChangedEventArgs args = new DependencyPropertyChangedEventArgs(ContentProperty, e.OldValue, e.NewValue);
frame.ContentChanged(frame, args);
}
}
}
}
In XAML I use it look like this:
<local:MyFrame ContentChanged="MyFrame_ContentChanged" />
The problem is that eventually I need to create an event handler MyFrame_ContentChanged
in the code-behind. Is there a way to do this in pure XAML? For example - can I convert the ContentChanged
dependency property to some sort of routed event?