WPF 重要新概念 之 路由事件的实现

来源:互联网 发布:java中multiple 编辑:程序博客网 时间:2024/05/16 17:29
    public class ButtonEvent : ButtonBase    {        //依赖属性        public static readonly RoutedEvent TestRoutedEvent;        public event RoutedEventHandler TestClick        {            add { AddHandler(ButtonEvent.TestRoutedEvent, value); }            remove { RemoveHandler(ButtonEvent.TestRoutedEvent, value); }        }        static ButtonEvent()        {            //注册属性            ButtonEvent.TestRoutedEvent = EventManager.RegisterRoutedEvent("TestClick", RoutingStrategy.Direct,                typeof(RoutedEventHandler), typeof(ButtonEvent));            // RoutingStrategy.Direct 指定路由事件 冒泡策略 从上到下 从下到上 不执行冒泡等        }        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)        {            //触发路由事件            RaiseEvent(new RoutedEventArgs(ButtonEvent.TestRoutedEvent, this));        }    }


 

绑定事件

  public MainWindow()        {            InitializeComponent();            btnGo.Click+=new RoutedEventHandler(btnGo_Click);        }        void btnGo_Click(object sender, RoutedEventArgs e)        {            MessageBox.Show("Test RoutedEventHandler");        }


 

绑定特类型的路由事件

       public MainWindow()        {            InitializeComponent();            this.AddHandler(Button.ClickEvent, new RoutedEventHandler(btnGo_Click));        }        void btnGo_Click(object sender, RoutedEventArgs e)        {            MessageBox.Show("Test RoutedEventHandler");        }


 

原创粉丝点击