日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF Demo18 路由事件

發布時間:2023/12/20 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 路由事件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。