WPF控件自定义点击事件

WPF部分控件没有点击事件,点击,指的是鼠标按下,然后抬起,形成一次点击,这里写了个类,实现了点击事件如下

class ClickEventAction
    {
        static List<ClickEventAction> clickEventActions = new List<ClickEventAction>();

        public static void AddClickEventAction(FrameworkElement frameworkElement, Action<object> action = null)
        {
            if (frameworkElement == null) return;
            clickEventActions.Add(new ClickEventAction(frameworkElement, action));
        }

        public static void RemoveClickEventAction(FrameworkElement frameworkElement)
        {
            foreach (ClickEventAction item in clickEventActions)
            {
                if(item.FrameworkElement == frameworkElement)
                {
                    item.FrameworkElement.MouseLeave -= item.UIElement_MouseLeave;
                    item.FrameworkElement.MouseLeftButtonUp -= item.UIElement_MouseLeftButtonUp;
                    item.FrameworkElement.MouseLeftButtonDown -= item.UIElement_MouseLeftButtonDown;
                    item.FrameworkElement.MouseEnter -= item.FrameworkElement_MouseEnter;
                }
            }
        }

        public ClickEventAction(FrameworkElement frameworkElement, Action<object> action = null)
        {
            FrameworkElement = frameworkElement;
            Action = action;
            frameworkElement.MouseLeftButtonDown += UIElement_MouseLeftButtonDown;
            frameworkElement.MouseLeftButtonUp += UIElement_MouseLeftButtonUp;
            frameworkElement.MouseLeave += UIElement_MouseLeave;
            frameworkElement.MouseEnter += FrameworkElement_MouseEnter;
        }

        private Brush background = null;

        public void FrameworkElement_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {

        }

        private bool isMouseDown = false;

        public void UIElement_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            isMouseDown = false;
        }

        public void UIElement_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (isMouseDown)
                Action?.Invoke(FrameworkElement);
            isMouseDown = false;
        }


        public void UIElement_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isMouseDown = true;
        }

        public Action<object> Action { get; set; }
        public FrameworkElement FrameworkElement { get; set; }
    }

使用方式,后台调用该类添加点击事件,Click为响应事件的方法

    ClickEventAction.AddClickEventAction(TableBorder, Click);

        private void Click(object obj)
        {
            if(obj is FrameworkElement framework)
            {
                switch (framework.Name)
                {
                    default:
                        break;
                }
            }
        }

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值