WPF Demo18 路由事件
生活随笔
收集整理的這篇文章主要介紹了
WPF Demo18 路由事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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);}} }?
轉載于:https://www.cnblogs.com/YYkun/p/6873087.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的WPF Demo18 路由事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开发移动端web页面click事件失效问
- 下一篇: 免费建站有哪些?