当前位置: 首页 > article >正文

自定义事件wpf

// 自定义控件

public class MyCustomControl : Control

{

    public static readonly RoutedEvent MyCustomEvent = EventManager.RegisterRoutedEvent(

        "MyCustom",

        RoutingStrategy.Bubbling,

        typeof(RoutedEventHandler),

        typeof(MyCustomControl)

    );

 

    public event RoutedEventHandler MyCustom

    {

        add { AddHandler(MyCustomEvent, value); }

        remove { RemoveHandler(MyCustomEvent, value); }

    }

 

  protected void OnMyCustom()

    {

        using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

 

public class MyCustomControl : Control

{

    public static readonly RoutedEvent MyCustomEvent = EventManager.RegisterRoutedEvent(

        "MyCustom",

        RoutingStrategy.Bubbling,

        typeof(RoutedEventHandler),

        typeof(MyCustomControl)

    );

 

    public event RoutedEventHandler MyCustom

    {

        add { AddHandler(MyCustomEvent, value); }

        remove { RemoveHandler(MyCustomEvent, value); }

    }

 

    protected void OnMyCustom(MouseEventArgs e)

    {

        // 获取鼠标相对于当前控件的坐标

        Point mousePosition = e.GetPosition(this);

 

        // 创建自定义事件参数,包含鼠标坐标

        var args = new RoutedEventArgs(MyCustomEvent)

        {

            Source = this

        };

 

        // 将鼠标坐标存储在事件参数的附加属性中

        args.SetData("MousePosition", mousePosition);

 

        // 触发事件

        RaiseEvent(args);

    }

 

 

          protected override void

OnMouseLeftButtonDown(MouseButtonEventArgs e)

    {

        base.OnMouseLeftButtonDown(e);

  (加触发条件)  鼠标类的事件通常在

 

mouseenter 或者onmouseleftbuttondown

的基础上改触发事件

point pos=mouse.getposition(某个控件)相对某个控件的坐标

 

键盘就是keydown上改e.key==key.enter 

        OnMyCustom();

    }

 

    static MyCustomControl()

    {

        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));

    }

}


<Window x:Class="MyApplication.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:MyApplication"

        Title="MainWindow" Height="350" Width="525">

    <Grid>

        <local:MyCustomControl MyCustom="MyCustomControl_MyCustom" />

    </Grid>

</Window>

private void MyCustomControl_MyCustom(object sender, RoutedEventArgs e)
{
    // 从事件参数中获取鼠标坐标
    if (e.GetData("MousePosition") is Point mousePosition)
    {
        MessageBox.Show($"Mouse Position: X = {mousePosition.X}, Y = {mousePosition.Y}");
    }
}
 

 

http://www.lryc.cn/news/2402682.html

相关文章:

  • ​​TLV4062-Q1​​、TLV4082-Q1​​迟滞电压比较器应用笔记
  • C++.OpenGL (3/64)着色器(Shader)深入
  • DHCP介绍
  • 李沐《动手学深度学习》d2l安装教程
  • [蓝桥杯]耐摔指数
  • 深入理解数字音频:采样率、位深与量化
  • 2024年第十五届蓝桥杯青少Scratch初级组-国赛—画矩形
  • java面试场景题: 设计⼀个微博系统
  • 市面上哪款AI开源软件做ppt最好?
  • JMM初学
  • transformer和 RNN以及他的几个变体区别 改进
  • 构建云原生安全治理体系:挑战、策略与实践路径
  • vue-print-nb 打印相关问题
  • vcs仿真产生fsdb波形的两种方式
  • 每日算法 -【Swift 算法】三数之和
  • Go语言底层(三): sync 锁 与 对象池
  • 登高架设作业操作证考试:理论题库高频考点有哪些?
  • 2025年06月06日Github流行趋势
  • 华为云CentOS配置在线yum源,连接公网后,逐步复制粘贴,看好自己对应的版本即可,【新手必看】
  • http头部注入攻击
  • 三类 Telegram 账号的风控差异分析与使用建议
  • Matlab | matlab中的点云处理详解
  • 【机试题解法笔记】寻找最大价值的矿堆
  • 动态规划 熟悉30题 ---上
  • 嵌入式学习笔记- freeRTOS 带FromISR后缀的函数
  • Linux系统:ELF文件的定义与加载以及动静态链接
  • 迷宫与陷阱--bfs+回路+剪枝
  • 【国产化适配】如何选择高效合规的安全数据交换系统?
  • 基于深度学习的裂缝检测与分割研究方向的 数据集介绍
  • 【Prompt实战】国际翻译小组