WPF之定义路由事件 http://www.51zxw.net/study.asp?vip=8963801

 2023-09-05 阅读 146 评论 0

摘要:[url=http://www.51zxw.net/study.asp?vip=8963801] 学IT必看,【经典】 【基础】 【进阶】 【实践 】 【成神】【超神】【返璞归真】 [/url] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using S
[url=http://www.51zxw.net/study.asp?vip=8963801] 学IT必看,【经典】 【基础】 【进阶】 【实践 】 【成神】【超神】【返璞归真】
[/url]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace WPFRoutedEvent
{
    public class RoutedEventEx : Button
    {
/// <summary>
        /// 定义路由事件
        /// </summary>
        public static readonly RoutedEvent ClickEvent;

        #region 路由事件类型  RoutingStrategy
        摘要:
             指示路由事件的路由
        //public enum RoutingStrategy
        //{
        //    // 摘要:
        //    //     路由事件使用隧道策略,以便事件实例通过树向下路由(从根到源元素)。
        //    Tunnel = 0,
        //    //
        //    // 摘要:
        //    //     路由事件使用冒泡策略,以便事件实例通过树向上路由(从事件元素到根)。
        //    Bubble = 1,
        //    //
        //    // 摘要:
        //    //     路由事件不通过元素树路由,但支持其他路由事件功能,例如类处理、System.Windows.EventTrigger 或 System.Windows.EventSetter。
        //    Direct = 2,
        //}
        #endregion
      
/// <summary>
        /// 用静态的构造函数 注册路由事件
        /// </summary>
        static RoutedEventEx()
        {
            RoutedEventEx.ClickEvent = EventManager.RegisterRoutedEvent("ClickEx", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RoutedEventEx));
        }
/// <summary>
        /// 封装路由事件 从而使所有.NET语言都能访问
        /// </summary>
        public event RoutedEventHandler ClickEx
        {
            add
            {
                AddHandler(RoutedEventEx.ClickEvent, value);
            }
            remove
            {
                RemoveHandler(RoutedEventEx.ClickEvent, value);
            }
        }
//AddHandler   RemoveHandler 这两个方法是FrameworkElement 基类中定义

       //共享路由事件
      //   UIElement.MouseUpEvent = Mouse.MouseUpEvent.AddWwner(typeof(UIElement));
//引发路由事件
        private void RaiseEventEx()
        {
            RoutedEventArgs e = new RoutedEventArgs(RoutedEventEx.ClickEvent, this);
            base.RaiseEvent(e);
        }
    }
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/103.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息