using System.Windows;

namespace 路由事件2
{
public class Student
{
////声明并定义路由事件
//public static readonly RoutedEvent NameChangedEvent =
// EventManager.RegisterRoutedEvent("NameChanged",
// RoutingStrategy.Bubble,
// typeof(RoutedEventHandler),
// typeof(Student)); private int id; public int Id
{
get { return id; }
set { id = value; }
}
private string name; public string Name
{
get { return name; }
set { name = value; }
}
}
}
<Window x:Class="路由事件2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="testGrid">
<Button x:Name="btnTest" Content="ok" Width="80" Height="75" FontSize="18" Click="btnTest_Click"/>
</Grid>
</Window>
using System.Windows;

namespace 路由事件2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//声明并定义路由事件
public static readonly RoutedEvent NameChangedEvent =
EventManager.RegisterRoutedEvent("NameChanged",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(MainWindow)); public MainWindow()
{
InitializeComponent(); //为grid添加路由事件侦听器
this.testGrid.AddHandler(NameChangedEvent, new RoutedEventHandler(StudentNameChangeEvent));
} private void btnTest_Click(object sender, RoutedEventArgs e)
{
Student stu = new Student()
{
Id = 1,
Name = "name001"
}; stu.Name = "name007"; //准备事件消息并发送路由事件
RoutedEventArgs arg = new RoutedEventArgs(NameChangedEvent, stu);
//RaiseEvent用于触发路由事件
this.btnTest.RaiseEvent(arg);
} public void StudentNameChangeEvent(object sender, RoutedEventArgs e)
{
MessageBox.Show("Id==" + (e.OriginalSource as Student).Id.ToString()
+ "\n"
+ "name==" + (e.OriginalSource as Student).Name.ToString());
}
}
}

实例二:

<Window x:Class="路由事件3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="testGrid">
<Button x:Name="btnTest" Content="ok" Width="80" Height="75" FontSize="18" Click="btnTest_Click"/>
</Grid>
</Window>
using System.Windows;

namespace 路由事件3
{
public class Student
{
//声明并定义路由事件
public static readonly RoutedEvent NameChangedEvent = EventManager.RegisterRoutedEvent
("NameChange",RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(Student)); //为界面元素添加路由侦听器
public static void AddNameChangedHandler(DependencyObject d,RoutedEventHandler h)
{
UIElement e = d as UIElement;
if (e != null) e.AddHandler(Student.NameChangedEvent, h);
} //移除侦听
public static void RemoveNameChangedHandler(DependencyObject d, RoutedEventHandler h)
{
UIElement e = d as UIElement;
if (e != null) e.RemoveHandler(Student.NameChangedEvent, h);
} private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
} using System.Windows; namespace 路由事件3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); //为外层Grid添加路由事件侦听器
Student.AddNameChangedHandler(this.testGrid,new RoutedEventHandler(NameChangedEvent));
} public void NameChangedEvent(object sender,RoutedEventArgs e)
{
MessageBox.Show("Id==" + (e.OriginalSource as Student).Id.ToString()
+ "\n"
+ "name==" + (e.OriginalSource as Student).Name.ToString());
} private void btnTest_Click(object sender, RoutedEventArgs e)
{
Student stu = new Student()
{
Id = 1,
Name = "001"
}; stu.Name = "002"; //准备事件消息并发送路由事件
RoutedEventArgs arg = new RoutedEventArgs(Student.NameChangedEvent, stu);
this.btnTest.RaiseEvent(arg);
}
}
}

WPF Demo18 路由事件的更多相关文章

  1. WPF - 善用路由事件

    原文:WPF - 善用路由事件 在原来的公司中,编写自定义控件是常常遇到的任务.但这些控件常常拥有一个不怎么好的特点:无论是内部还是外部都没有使用路由事件.那我们应该怎样宰自定义控件开发中使用路由事件 ...

  2. WPF的路由事件、冒泡事件、隧道事件(预览事件)

    本文摘要: 1:什么是路由事件: 2:中断事件路由: 3:自定义路由事件: 4:为什么需要自定义路由事件: 5:什么是冒泡事件和预览事件(隧道事件): 1:什么是路由事件 WPF中的事件为路由事件,所 ...

  3. WPF:自定义路由事件的实现

    路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...

  4. 学习WPF——了解路由事件

    入门 我们先来看一个例子 前台代码: 后台代码: 点击按钮的运行效果第一个弹出窗口 第二个弹出窗口: 第三个弹出窗口: 说明 当点击按钮之后,先触发按钮的click事件,再上查找,发现stackpan ...

  5. 【WPF】路由事件

    总结WPF中的路由事件,我将学到的内容分为四部分来逐渐掌握 第一部分:wpf中内置的路由事件 以Button的Click事件来说明内置路由事件的使用 XAML代码: <Window x:Clas ...

  6. 迟到的 WPF 学习 —— 路由事件

    1. 理解路由事件:WPF 通过事件路由(event routing)概念增强了传统的事件执行的能力和范围,允许源自某个元素的事件由另一个元素引发,例如,事件路由允许工具栏上的一个按钮点击的事件在被代 ...

  7. WPF自定义路由事件(二)

    WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 WPF ...

  8. WPF中路由事件的传播

    路由事件(RoutedEvent)是WPF中新增的事件,使用起来与传统的事件差别不大, 但传播方式是完全不同的. 路由事件的传播方式 通过RoutingStrategy来定义传播的方式 public ...

  9. WPF 冒泡路由事件

    在WPF中,例如,可以构建一个包含图形的按钮,创建一个具有文本和图片混合内容的标签,或者为了实现滚动或折叠的显示效果在一个特定的容器中放置内容.甚至可以多此重复嵌套,直到达到您所希望的层次深度. 这种 ...

随机推荐

  1. awr脚本使用dump导出导入

    实际工作中,存在这么一种场景.客户现场分析问题,无法立即得出结论,且无法远程服务器,因此对于服务器中的awr信息,如何提取是一个问题,oracle有脚本可以对服务器中以db为单位导出awr基表的dum ...

  2. c++字符串前几位,后几位的截取

    参考 1. https://blog.csdn.net/haijunsm/article/details/82733584 完

  3. How did I Install DCGAN

    https://github.com/carpedm20/DCGAN-tensorflow how to install pillow(python image library) http://www ...

  4. Dependency Parsing -13 chapter(Speech and Language Processing)

    https://web.stanford.edu/~jurafsky/slp3/13.pdf constituent-based 基于成分的phrasal constituents and phras ...

  5. 【递推】【HDOJ】

    http://acm.hdu.edu.cn/showproblem.php?pid=2501 Tiling_easy version Time Limit: 1000/1000 MS (Java/Ot ...

  6. 【Jmeter】插件

    一.插件管理 前提:很多时候,尤其是性能测试的时候,我们需要用到很多场景,需要得到一些参数值等等. 二.插件地址 URL : https://jmeter-plugins.org/downloads/ ...

  7. Js 字符串拼接的两种方法

    字符串拼接的两种方法 用数组的方法的好处是:避免变量重新定义.赋值 <!DOCTYPE html> <html lang="en"> <head> ...

  8. prufer数列

    涨姿势---prufer数列 一. 简介 Prufer数列是无根树的一种数列.在组合数学中,Prufer数列由有一个对于顶点标过号的树转化来的数列,点数为n的树转化来的Prufer数列长度为n-2.它 ...

  9. mac-ppt-auto-open-recovery-files

    mac 每次打开PPT都会出现一个自动保存的文件,不知道这个文件是保存在哪里,该怎么删除 打开finder(访达),按 shift+command+G,输入~/Library/Containers/c ...

  10. 获取和设置用户id以及组id

    #include<unistd.h> uid_t getuid(void); uid_t geteuid(void);//获取有效用户id gid_t getgid(void); gid_ ...