• 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(ChooseDrugRoutedEventArgs), 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");
            }
        }
    }
  • 相关阅读:
    搭建一个免费的,无限流量的Bloggithub Pages和Jekyll入门
    通过扩展方法 链式方法 为MVC 3 视图添加验证
    让移动UI模式设计者获得灵感的10个有用的网站资源
    企业级应用架构(NHibernater+Spring.Net+MVC3)_V1.0
    把博客放在Github
    实例化需求—流程
    nginx+keepalievd,实现负载均衡和故障点切换。keepalived双机热备。
    古城钟楼
    从初步使用该控件到多维数据集控件PivotGridControl
    搜索引擎的评价
  • 原文地址:https://www.cnblogs.com/vipsoft/p/7020966.html
Copyright © 2020-2023  润新知