打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
深入浅出WPF第二部分(17)8.3.3RoutedEventArgs的Source和Origin
希赛网 > 问答 > 网络工程 > 物联网与云计算 > 云计算 > 深入浅出WPF第二部分(17)8.3.3RoutedEventArgs的Source和Origin

深入浅出WPF第二部分(17)8.3.3RoutedEventArgs的Source和Origin

提问者: mingzhu17     发布时间:2014-04-15     浏览:25     回复:0     悬赏:0.0希赛币

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

  8.3.3 RoutedEventArgs的Source和OriginalSource

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

  <UserControl x:Class="FirstWpfApplication.Controls.UserControl1" xmlns="; xmlns:x="; xmlns:mc="; xmlns:d="; mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Button x:Name="innerButton" Width="80" Height="80" Content="OK"/> </Grid></UserControl>

  <Window x:Class="FirstWpfApplication.MainWindow" x:ClassModifier="public" xmlns="; xmlns:x="; xmlns:data="clr-namespace:System.Windows.Data;assembly=PresentationFramework" xmlns:local="clr-namespace:FirstWpfApplication" xmlns:controls="clr-namespace:FirstWpfApplication.Controls" Title="登录" Height="300" Width="300" > <Window.Resources> </Window.Resources> <Grid x:Name="grid"> <controls:UserControl1 x:Name="myUserControl" Margin="10" /> </Grid></Window>

  /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.Button_Click)); } private void Button_Click(object sender, RoutedEventArgs e) { string strOriginalSource = string.Format("VirtualTree start point:{0}, type is {1}", (e.OriginalSource as FrameworkElement).Name, e.OriginalSource.GetType().ToString()); string strSource = string.Format("LogicalTree start point:{0}, type is {1}", (e.Source as FrameworkElement).Name, e.Source.GetType().ToString()); MessageBox.Show(strOriginalSource + Environment.NewLine + strSource); } }


8.3.4 事件也附加——深入浅出附加附件

  路由事件的宿主都是些拥有可视化实体的界面元素,而附加事件则不具备显示在用户界面上的能力。也就是说,附加事件的宿主没有界面渲染功能这双“飞翼”,但一样可以使用附加事件这个灵犀与其他对象进行沟通。

  class Student { public static readonly RoutedEvent NameChangedEvent = EventManager.RegisterRoutedEvent ("NameChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Student)); public int Id { get; set; } public string Name { get; set; } }


  <Grid x:Name="grid"> <Button x:Name="button1" Content="OK" Width="80" Height="80" Click="Button_Click"/> </Grid>


  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方法把事件发送出去;事件的侦听任务也只能交给别的对象去做。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
WPF简单导航框架(Window与Page互相调用)
WPF 使用WindowChrome自定义窗体 保留原生窗体特性
C# WPF遮罩对话框(Popup Message Overlay/ Dialog Host)
C# 一个基于.NET Core3.1的开源项目帮你彻底搞懂WPF框架Prism
如何使用WPF做一个变频器调试软件
WPF中查看PDF文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服