WPF Demo18 路由事件
阿新 • • 發佈:2017-05-18
mov message strategy res con ini img err mage
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 路由事件