WPF自定义RoutedEvent事件示例代码

在这两年工作中的项目中越来越多的接触WPF控件的开发工作,同样也大量的用到了WPF的RoutedEvent事件,但每次需要编写的时候,总要翻以前的代码,所以在这里直接记录下来,免得自己再查来查去了。。

        public static readonly DependencyProperty PageIndexProperty =
            DependencyProperty.Register("PageIndex", typeof(int), typeof(MoonPdfPanel), new UIPropertyMetadata(1));

        public int PageIndex
        {
            get {return (int)GetValue(PageIndexProperty);}
            set { SetValue(PageIndexProperty, value); }
        }

        public static readonly RoutedEvent PageIndexChangedEvent = EventManager.RegisterRoutedEvent("PageIndexChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<int>), typeof(MoonPdfPanel));

        public event RoutedPropertyChangedEventHandler<int> PageIndexChanged
        {
            add { AddHandler(PageIndexChangedEvent, value); }
            remove { RemoveHandler(PageIndexChangedEvent, value); }
        }

        // 触发事件,结尾一定要使用RaiseEvent(args)来触发路由事件。
        RoutedPropertyChangedEventArgs<int> args =
                new RoutedPropertyChangedEventArgs<int>(
                    val, this.GetCurrentPageNumber(), PageIndexChangedEvent);
            this.RaiseEvent(args);

 

posted @ 2013-07-22 16:13  star丶清影  阅读(6479)  评论(0编辑  收藏  举报