WPF Demo18 路由事件

using System.Windows;

namespace 路由事件2
{
    public class Student
    {
        ////声明并定义路由事件
        //public static readonly RoutedEvent NameChangedEvent =
        //    EventManager.RegisterRoutedEvent("NameChanged",
        //    RoutingStrategy.Bubble,
        //    typeof(RoutedEventHandler), 
        //    typeof(Student));

        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}
<Window x:Class="路由事件2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="testGrid">
        <Button x:Name="btnTest"  Content="ok" Width="80"  Height="75" FontSize="18" Click="btnTest_Click"/>
    </Grid>
</Window>
using System.Windows;

namespace 路由事件2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //声明并定义路由事件
        public static readonly RoutedEvent NameChangedEvent =
            EventManager.RegisterRoutedEvent("NameChanged",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(MainWindow));

        public MainWindow()
        {
            InitializeComponent();

            //为grid添加路由事件侦听器
            this.testGrid.AddHandler(NameChangedEvent, new RoutedEventHandler(StudentNameChangeEvent));
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            Student stu = new Student()
            {
                Id = 1,
                Name = "name001"
            };

            stu.Name = "name007";

            //准备事件消息并发送路由事件
            RoutedEventArgs arg = new RoutedEventArgs(NameChangedEvent, stu);
            //RaiseEvent用于触发路由事件
            this.btnTest.RaiseEvent(arg);
        }

        public void StudentNameChangeEvent(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Id==" + (e.OriginalSource as Student).Id.ToString()
                + "
"
                + "name==" + (e.OriginalSource as Student).Name.ToString());
        }
    }
}

实例二:

<Window x:Class="路由事件3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="testGrid">
        <Button x:Name="btnTest" Content="ok" Width="80" Height="75" FontSize="18" Click="btnTest_Click"/>
    </Grid>
</Window>
using System.Windows;

namespace 路由事件3
{
    public class Student
    {
        //声明并定义路由事件
        public static readonly RoutedEvent NameChangedEvent = EventManager.RegisterRoutedEvent
            ("NameChange",RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(Student));

        //为界面元素添加路由侦听器
        public static void AddNameChangedHandler(DependencyObject d,RoutedEventHandler h) 
        {
            UIElement e = d as UIElement;
            if (e != null) e.AddHandler(Student.NameChangedEvent, h);
        }

        //移除侦听
        public static void RemoveNameChangedHandler(DependencyObject d, RoutedEventHandler h)
        {
            UIElement e = d as UIElement;
            if (e != null) e.RemoveHandler(Student.NameChangedEvent, h);
        }

        private int id;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}


using System.Windows;

namespace 路由事件3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //为外层Grid添加路由事件侦听器
            Student.AddNameChangedHandler(this.testGrid,new RoutedEventHandler(NameChangedEvent));
        }

        public void NameChangedEvent(object sender,RoutedEventArgs e)
        {
            MessageBox.Show("Id==" + (e.OriginalSource as Student).Id.ToString()
               + "
"
               + "name==" + (e.OriginalSource as Student).Name.ToString());
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            Student stu = new Student()
            {
                Id = 1,
                Name = "001"
            };

            stu.Name = "002";

            //准备事件消息并发送路由事件
            RoutedEventArgs arg = new RoutedEventArgs(Student.NameChangedEvent, stu);
            this.btnTest.RaiseEvent(arg);
        }
    }
}

原文地址:https://www.cnblogs.com/YYkun/p/6873087.html