其他分享
首页 > 其他分享> > WPF 附加事件

WPF 附加事件

作者:互联网

路由事件的宿主是那些有 UI 显示功能的界面元素,而附加事件是那些没有 UI 显示功能的元素,其本质还是路由事件,只是路由事件的宿主不一样。附加事件只是路由事件的一种用法而已。

XAML:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300">
    <Grid x:Name="gridMain">
        <Button x:Name="button1" Content="OK" Width="80" Height="80" Click="Button1_Click"/>
    </Grid>
</Window>

▲ 只做了一个按钮触发一下

附加事件宿主,Student 类:

public class Student
{
    public static readonly RoutedEvent NameChangedEvent = EventManager.RegisterRoutedEvent
        ("NameChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Student));

    public int Id { get; set; }
    public string Name { get; set; }
}

C# behind 代码:

/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        gridMain.AddHandler(Student.NameChangedEvent, new RoutedEventHandler(StudentNameChangedHandler));
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        Student stu = new Student() { Id=10101, Name = "Tim" };
        stu.Name = "Tom";
        // 准备事件消息,并发送路由事件
        RoutedEventArgs args = new RoutedEventArgs(Student.NameChangedEvent, stu);
        button1.RaiseEvent(args);  // 借用下 RaiseEvent 函数,借用 gridMain 的也可以
    }

    // Grid 捕捉到 NameChangedEvent 后的处理器
    private void StudentNameChangedHandler(object sender, RoutedEventArgs e)
    {
        MessageBox.Show((e.OriginalSource as Student).Id.ToString());
    }
}

理论上,上面已经算一个附加事件了。

▲ 点击按钮,弹出学号

因为 Student 不是继承 UIElement 类,所以,没有 AddHandler 和 RemoveHandler 两个方法,也没有哦 RiaseEvent 方法。

根据微软规定,可以升级一下:

Student 类:

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

    /// <summary>
    /// 为界面元素增加路由事件监听
    /// </summary>
    /// <param name="d">事件的监听者</param>
    /// <param name="h">事件处理器委托类型</param>
    public static void AddNameChangedHandler(DependencyObject d, RoutedEventHandler h)
    {
        UIElement uIElement = d as UIElement;
        if (uIElement != null)
        {
            uIElement.AddHandler(Student.NameChangedEvent, h);
        }
    }

    /// <summary>
    /// 移除侦听
    /// </summary>
    /// <param name="d">事件的监听者</param>
    /// <param name="h">事件处理器委托类型</param>
    public static void RemoveNameChangedHandler(DependencyObject d, RoutedEventHandler h)
    {
        UIElement uIElement = d as UIElement;
        if (uIElement != null)
        {
            uIElement.RemoveHandler(Student.NameChangedEvent, h);
        }
    }

    public int Id { get; set; }
    public string Name { get; set; }
}

C# Behind:

/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // 这个地方改一下
        Student.AddNameChangedHandler(gridMain, new RoutedEventHandler(StudentNameChangedHandler));
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        Student stu = new Student() { Id = 10101, Name = "Tim" };
        stu.Name = "Tom";
        // 准备事件消息,并发送路由事件
        RoutedEventArgs args = new RoutedEventArgs(Student.NameChangedEvent, stu);
        button1.RaiseEvent(args);
    }

    // Grid 捕捉到 NameChangedEvent 后的处理器
    private void StudentNameChangedHandler(object sender, RoutedEventArgs e)
    {
        MessageBox.Show((e.OriginalSource as Student).Id.ToString());
    }
}

附加事件,只是路由事件的一种用法,而非一个新概念。

标签:RoutedEventArgs,附加,NameChangedEvent,事件,Student,WPF,public,路由
来源: https://www.cnblogs.com/lzjsky/p/16128726.html