简体   繁体   English

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();

I want a storyboard to begin when the page has changed, and I want to do it in XAML, and not in code-behind. 我希望在页面发生变化时开始使用故事板,我想在XAML中进行,而不是在代码隐藏中。 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 label control that provides a ContentChanged event. 最动态的方法是简单地派生自己的标签控件,该控件提供ContentChanged事件。

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: 在XAML中,我使用它看起来像这样:

<local:MyFrame ContentChanged="MyFrame_ContentChanged" />

The problem is that eventually I need to create an event handler MyFrame_ContentChanged in the code-behind. 问题是最终我需要在代码隐藏中创建一个事件处理程序MyFrame_ContentChanged Is there a way to do this in pure XAML? 有没有办法在纯XAML中执行此操作? For example - can I convert the ContentChanged dependency property to some sort of routed event? 例如 - 我可以将ContentChanged依赖项属性转换为某种路由事件吗?

In order to use events with EventTriggers they should be routed events. 为了将事件与EventTriggers一起使用,它们应该是路由事件。 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 . 以下是如何入门的快速教程: 如何:创建自定义路由事件

Here's an example of a class deriving from ContentControl which defines a ContentChanged event: 以下是从ContentControl派生的类的示例,它定义了ContentChanged事件:

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));
    }
}

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: 我还不确定为什么,但是测试此行在Style ,但在控件的触发器集合中直接使用时抛出异常:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM