WPF 自定义路由事件
1.可传参数
namespace DrugInfo { public class ChooseDrugRoutedEventArgs : RoutedEventArgs { public ChooseDrugRoutedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { } public string Message { get; set; } } public partial class ChooseDrug { //声明和注册路由事件 public static readonly RoutedEvent ChooseDrugRoutedEvent = EventManager.RegisterRoutedEvent("ChooseDrug", RoutingStrategy.Bubble, typeof(ChooseDrugRoutedEventHandler), typeof(ChooseDrug)); //自定义事件委托 public delegate void ChooseDrugRoutedEventHandler(object sender, ChooseDrugRoutedEventArgs e); //CLR事件包装 public event ChooseDrugRoutedEventHandler ChooseDrugClick { add { AddHandler(ChooseDrugRoutedEvent, value); } remove { RemoveHandler(ChooseDrugRoutedEvent, value); } } //界面选择数据后确定 private void Choose_OnClick(object sender, RoutedEventArgs routedEventArgs) { ChooseDrugRoutedEventArgs args = new ChooseDrugRoutedEventArgs(ChooseDrugRoutedEvent, this); args.Message = "传数据过去"; this.RaiseEvent(args);//UIElement及其派生类 } } } //调用 namespace Purchasing { /// <summary> /// Main.xaml 的交互逻辑 /// </summary> public partial class Main { private void Control_Loaded(object sender, RoutedEventArgs e) { var popups = new ChooseDrug(); popups.ChooseDrugClick += Popups_ChooseDrugClick; } private void Popups_ChooseDrugClick(object sender, ChooseDrugRoutedEventArgs e) { RadWindow.Alert(e.Message); } } }
2.无参数,不需要自定义委托
1.可传参数 namespace DrugInfo { public partial class ChooseDrug { //声明和注册路由事件 public static readonly RoutedEvent ChooseDrugRoutedEvent = EventManager.RegisterRoutedEvent("ChooseDrug", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventHandler>), typeof(ChooseDrug)); //CLR事件包装 public event RoutedEventHandler ChooseDrugClick { add { AddHandler(ChooseDrugRoutedEvent, value); } remove { RemoveHandler(ChooseDrugRoutedEvent, value); } } //界面选择数据后确定 private void Choose_OnClick(object sender, RoutedEventArgs routedEventArgs) { RaiseEvent(new RoutedEventArgs(ChooseDrugRoutedEvent)); } } } //调用 namespace Purchasing { /// <summary> /// Main.xaml 的交互逻辑 /// </summary> public partial class Main { private void Control_Loaded(object sender, RoutedEventArgs e) { var popups = new ChooseDrug(); popups.ChooseDrugClick += Popups_ChooseDrugClick; } private void Popups_ChooseDrugClick(object sender, RoutedEventArgs e) { RadWindow.Alert("OK"); } } }
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/7020966.html