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

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

相关文章:

  • 构建云原生安全治理体系:挑战、策略与实践路径
  • 解锁FastAPI与MongoDB聚合管道的性能奥秘
  • 动态规划(3)
  • 开关机、重启、改密、登录:图解腾讯云CVM日常管理核心操作,轻松掌控你的云主机
  • 【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
  • App使用webview套壳引入h5(二)—— app内访问h5,顶部被手机顶部菜单遮挡问题,保留顶部安全距离
  • nano编辑器的详细使用教程
  • 结合PDE反应扩散方程与物理信息神经网络(PINN)进行稀疏数据预测的技术方案
  • Spring Boot + MyBatis 集成支付宝支付流程
  • GIT - 如何从某个分支的 commit创建一个新的分支?
  • Arduino学习-按键灯
  • 智慧充电:新能源汽车智慧充电桩的发展前景受哪些因素影响?
  • ros2--图像/image
  • 各种排序算法的再整理
  • 新能源汽车智慧充电桩管理方案:新能源充电桩散热问题及消防安全监管方案
  • Java转Go日记(六十):gin其他常用知识
  • Angular报错:cann‘t bind to ngClass since it is‘t a known property of div
  • 电路图识图基础知识-自耦变压器降压启动电动机控制电路(十六)
  • 洛谷题目:P2761 软件补丁问题 (本题简单)
  • SD系列I/O接口cRBX01 2VAA008424R1
  • JavaSec-SSTI - 模板引擎注入
  • 深度学习学习率优化方法——pytorch中各类warm up策略
  • 桂花网蓝牙网关物联网医院动态血糖管理应用案例
  • Vue.js 组件:深入理解与实践
  • Spring Boot缓存组件Ehcache、Caffeine、Redis、Hazelcast
  • 使用 C/C++ 和 OpenCV 添加图片水印
  • Android协程学习
  • 负载均衡将https请求转发后端http服务报错:The plain HTTP request was sent to HTTPS port
  • 模块化架构下的前端调试体系建设:WebDebugX 与多工具协同的工程实践
  • 【图像处理3D】:焦距的像素单位标定