zoukankan      html  css  js  c++  java
  • WPF 给用户控件增加自定义事件的记录

    第一步 在自定义控件里加上声明

            /// <summary>
            /// The click event
            /// </summary>
            public static readonly RoutedEvent userControlClickEvent = EventManager.RegisterRoutedEvent("UserControlClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CommonButton));
    
            /// <summary>
            /// 控件点击的操作.
            /// </summary>
            public event RoutedEventHandler UserControlClick
            {
                add
                {
                    AddHandler(userControlClickEvent, value);
                }
    
                remove
                {
                    RemoveHandler(userControlClickEvent, value);
                }
            }

    第二步 给自定义控件的按钮的click里添加调用

            public void btnClick(object sender, RoutedEventArgs e)
            {
                RoutedEventArgs args = new RoutedEventArgs(userControlClickEvent, this);
                RaiseEvent(args);
            }

    第三步 外部调用该事件

                CommonButton button = new CommonButton();
                button.UserControlClick += Button_UserControlClick;
                button.Width = 200;
                button.Height = 200;
                button.ImagePathNormal = "/Images/LeftMenuButtons/left_menu_1_normal.png";
                button.ImagePathPressed = "/Images/LeftMenuButtons/left_menu_1_selected.png";
                button.SetValue(Canvas.ZIndexProperty, 999);
                button.SetValue(Canvas.LeftProperty, (double)0);
                button.SetValue(Canvas.TopProperty, (double)0);
    
                mainCanvas.Children.Add(button);
            private void Button_UserControlClick(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("已执行");
            }
  • 相关阅读:
    【原创】C++11:左值和右值(深度分析)
    【基础核心理论】运算符重载
    左值与右值引用 详解
    托盘图标编程
    C/C++ 数组与指针
    webpack 4.0改版问题
    mysql5.7安装记录
    equals方法
    【原创】IO流:读写操作研究(输入流)
    为什么重写equals一定要重写hashCode?
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/15455997.html
Copyright © 2011-2022 走看看