深入浅出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()); }