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

自定义事件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.xdnf.cn/news/12130.html

相关文章:

  • Zookeeper 和 Kafka 版本与 JDK 要求
  • Nginx部署vue项目, 无法直接访问其他路径的解决方案
  • JVM垃圾回收器-ZGC
  • nano编辑器的详细使用教程
  • 中达瑞和SHIS高光谱相机在黑色水彩笔墨迹鉴定中的应用
  • xmind转换为markdown
  • Numpy入门4——结构化数组和Numpy文件
  • C++多态与继承实战解析
  • 如何判断是 CPU 密集还是 IO 密集型任务?
  • C++语法系列之IO流
  • JAVA 集合进阶 01 - 05 双列集合
  • FEMFAT许可分析中的关键指标
  • Java直接内存(directMemory)分配与查看
  • DNS解析深入探讨
  • linux扫描所有私有网段shell脚本
  • 2.MySQL基础:SQL语句
  • 【Python实战】零基础实战教程(三) 变量与数据类型
  • 【Python指南】离线安装顽固复杂的第三方库指南
  • Transformers生成文本:max_new_tokens揭秘
  • 第二十三章 Shell的基础语法
  • haribote原型系统改进方向
  • 【Day44】
  • 【向量化模型如何私有化部署】一文说清原理、流程与最佳实践
  • 软件工程专业本科毕业论文模板
  • 龙虎榜——20250604
  • ‌RF Choke(射频扼流圈)
  • 2D 写实交互数字人:多终端实时交互,引领数字化浪潮
  • 告别延迟,拥抱速度:存储加速仿真应用的解决方案【1】
  • 【五子棋在线对战】二.项目结构设计 实用工具类模板的实现
  • 分享国外几个好用的ai网站