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