WPF:如何为内容更改创建路由事件? [英] WPF: How to created a routed event for content changed?
问题描述
我有一个框架.我用这一行切换页面:
I have a frame. I switch pages with this line:
FrameName.Content = new PageName();
我希望情节提要在页面更改时开始,并且我想在XAML中而不是在代码隐藏中进行.我尝试了以下代码:
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
最动态的方法是简单地派生您自己的提供ContentChanged事件的
label控件.
我已经尝试实现此答案中的代码:
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);
}
}
}
}
在XAML中,我使用它的外观如下:
In XAML I use it look like this:
<local:MyFrame ContentChanged="MyFrame_ContentChanged" />
问题在于,最终我需要在后面的代码中创建事件处理程序MyFrame_ContentChanged
.有没有办法在纯XAML中做到这一点?例如-我可以将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?
推荐答案
为了将事件与EventTriggers
一起使用,应将其路由.路由事件的定义方式与依赖项属性类似.这是有关如何入门的快速教程:如何:创建自定义路由事件.
In order to use events with EventTriggers
they should be routed events. Routed events are defined in a way similar to dependency properties. Here's a quick tutorial on how to get started: How to: Create a Custom Routed Event.
这是从ContentControl
派生的类的示例,该类定义了ContentChanged
事件:
Here's an example of a class deriving from ContentControl
which defines a ContentChanged
event:
public class MyContentControl : ContentControl
{
public static readonly RoutedEvent ContentChangedEvent
= EventManager.RegisterRoutedEvent(
"ContentChanged",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(MyContentControl));
public event RoutedEventHandler ContentChanged
{
add { AddHandler(ContentChangedEvent, value); }
remove { RemoveHandler(ContentChangedEvent, value); }
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
RaiseEvent(new RoutedEventArgs(ContentChangedEvent, this));
}
}
我不确定为什么,但是在测试此行时在Style
内部运行,但是直接在控件的触发器集合中使用时抛出了异常:
I'm not yet sure why, but while testing this line worked inside a Style
, but threw an exception while used in the control's triggers collection directly:
<EventTrigger RoutedEvent="ContentChanged">...</EventTrigger>
为了使其在这种情况下起作用,我必须指定一个完全限定的事件路径:
In order to make it work in this situation I had to specify a fully qualified event path:
<EventTrigger RoutedEvent="local:MyContentControl.ContentChanged">...</EventTrigger>
这篇关于WPF:如何为内容更改创建路由事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!