public class Person

{

public static readonly RoutedEvent NameChangedEvent = EventManager.RegisterRoutedEvent("NameChanged", RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(Person));

//为界面添加路由侦听

public static void AddNameChangedHandle(DependencyObject d,RoutedEventHandler h)

{

UIElement e = d as UIElement;

if(null!=e)

{

e.AddHandler(NameChangedEvent, h);

}

}

//移除侦听

public static void RemoveNameChangedHandle(DependencyObject d,RoutedEventHandler h)

{

UIElement e = d as UIElement;

if(null!=e)

{

e.RemoveHandler(NameChangedEvent,h);

}

}

public int Id { get; set; }

public string Name { get; set; }

}

<Window x:Class="WpfApplication1.Window27"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window27" Height="272" Width="349">

<Grid x:Name="gd_main">

<Button Content="Button"  x:Name="button1" Width="75" Height="75" Margin="10" Click="button1_Click" />

</Grid>

</Window>

public partial class Window27 : Window

{

public Window27()

{

InitializeComponent();

//为外层Grid添加路由事件

Person.AddNameChangedHandle(this.gd_main, new RoutedEventHandler(PersonNameChanged));

}

private void PersonNameChanged(object obj, RoutedEventArgs e)

{

MessageBox.Show((e.OriginalSource as Person).Name);

}

private void button1_Click(object sender, RoutedEventArgs e)

{

Person persion = new Person();

persion.Id = 0;

persion.Name = "Darren";

//准备事件消息并发送路由事件

RoutedEventArgs arg = new RoutedEventArgs(Person.NameChangedEvent, persion);

this.button1.RaiseEvent(arg);

}

}

最新文章

  1. hihocoder -1121-二分图的判定
  2. 通过挂载系统光盘搭建本地yum仓库的方法
  3. OpenCv Mat操作总结
  4. 三级联动(在YII框架中)
  5. 【python】类的访问限制
  6. [BTS]The join order has been enforced because a local join hint is used.;Duplicate key was ignored.&quot;.
  7. repo andrid
  8. java String与Byte[]和String 与InputStream转换时注意编码问题。。。
  9. Ubuntu系统下运行Eclipse出现找不到jre的问题的解决方法
  10. css style与class之间的区别
  11. Update与Mysql、Sqlsever中的随机数
  12. springmvc附件上传核心代码
  13. Jquey里的同步请求和异步请求
  14. netty的编解码器理解(转)
  15. python time模块和datetime模块
  16. ruby读取exce文件,使用roo---Gem
  17. Bootstrap如何配合字体自适应
  18. c++ __declspec
  19. Android UI系列-----RelativeLayout的相关属性
  20. MYSQL + MHA +keepalive + VIP安装配置(一)--MYSQL安装配置

热门文章

  1. python 14篇 写日志
  2. SHELL 变量引用
  3. Java基础00-方法10
  4. Spring RestTemplate 之get请求
  5. python -- 程序的结构语句
  6. IO流之节点流(字符流)和数据流关闭
  7. CH1809 匹配统计 题解
  8. [考试总结]noip模拟22
  9. 第十篇 -- 下拉列表框QComboBox
  10. python使用正则+jsonpath处理接口依赖