首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 云计算 >

深入显出WPF 第二部分(17)

2013-01-06 
深入浅出WPF 第二部分(17)8.3.3 RoutedEventArgs的Source和OriginalSourceRoutedEventArgs有两个属性Sourc

深入浅出WPF 第二部分(17)

8.3.3 RoutedEventArgs的Source和OriginalSource

RoutedEventArgs有两个属性Source和OriginalSource,这两个属性都表示路由事件传递的起点(即事件消息的源头),只不过Source表示的是LogicalTree上的消息源头,而OriginalSource则表示VirtualTree上的源头。

        public MainWindow()        {            InitializeComponent();            this.AddHandler(Student.NameChangedEvent, new RoutedEventHandler(this.StudentNameChangedHandler));        }        private void Button_Click(object sender, RoutedEventArgs e)        {            Student stu = new Student() { Id = 10, Name = "Tim" };            RoutedEventArgs arg = new RoutedEventArgs(Student.NameChangedEvent, stu);            this.button1.RaiseEvent(arg);        }        private void StudentNameChangedHandler(object sender, RoutedEventArgs e)        {            MessageBox.Show((e.OriginalSource as Student).Id.ToString());        }

确切地说,UIElement类是路由事件宿主与附加事件宿主的分水岭,不单是因为从UIElement类开始才具备了在界面上显示的能力,还因为RaiseEvent、AddHandler和RomveHandler这些方法也定义在UIElement类中。如果在一个非UIElement派生类中注册了路由事件,则这个类的实例即不能自己激发此路由事件也无法自己侦听此路由事件,只能把这个事件的激发“附着”在某个RaiseEvent方法的对象上,借助这个对象的RaiseEvent方法把事件发送出去;事件的侦听任务也只能交给别的对象去做。

热点排行