我无法使自定义点击事件正常工作。

Hui Liu-MSFT 40,666 信誉分 Microsoft 供应商
2024-04-18T06:05:54.2933333+00:00

我写了这个类。

public class MyButtonSimple : Button
{
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
        "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));

    // Provide CLR accessors for the event
    public event RoutedEventHandler Tap
    {
        add { AddHandler(TapEvent, value); }
        remove { RemoveHandler(TapEvent, value); }
    }

    // This method raises the Tap event
    void RaiseTapEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);
        RaiseEvent(newEventArgs);
    }
    // For demonstration purposes we raise the event when the MyButtonSimple is clicked
    protected override void OnClick()
    {
        RaiseTapEvent();
    }

它工作正常,我单击它,它显示一个 MessageBox。 问题是我想将它派生的类更改为“UserControl”。 而且我不希望它源自 Button。但是,当我 更改它并尝试单击它时,它不起作用,没有任何反应。

Note:此问题总结整理于:I can't get my custom click event to work.

Windows Presentation Foundation
Windows Presentation Foundation
.NET Framework 的一部分,它提供统一的编程模型,用于在 Windows 上构建业务线桌面应用程序。
57 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Jiale Xue - MSFT 34,201 信誉分 Microsoft 供应商
    2024-04-18T09:09:47.1233333+00:00

    OnClick 不属于 UserControl 方法,您可以根据需要覆盖任何 UserControl 方法。在我的测试中,我重写了 Method 以引发 UserControl 的 RaiseTapEvent,下面是我编辑的OnMouseLeftButtonDownMyButtonSimple.cs

     public class MyButtonSimple:UserControl  
        {  
            // Create a custom routed event by first registering a RoutedEventID  
            // This event uses the bubbling routing strategy  
            public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(  
                "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));  
      
            // Provide CLR accessors for the event  
            public event RoutedEventHandler Tap  
            {  
                add { AddHandler(TapEvent, value); }  
                remove { RemoveHandler(TapEvent, value); }  
            }  
            // This method raises the Tap event  
            void RaiseTapEvent()  
            {  
                RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);  
                RaiseEvent(newEventArgs);  
            }  
            // For demonstration purposes we raise the event when the MyButtonSimple is clicked  
      
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)  
            {  
                RaiseTapEvent();  
            }  
        }  
    

    若要在 MainWindow.xaml 中使用它,请执行以下操作:

      <local:MyButtonSimple Width="180" Height="30" Background="Aqua" Tap="MyButtonSample_Tab" ></local:MyButtonSimple>  
    

    MainWindow.xaml.cs代码为:

     private void MyButtonSample_Tab(object sender, RoutedEventArgs e)  
            {  
                MessageBox.Show("This is MyButtonSimple:UserControl.");  
            }  
    

    当我单击MyButtonSimple时,结果图片为: 42760-1.png

    这是我对你的问题的理解,如果我误解了,请详细指出并描述你的问题,这对我分析是有益的。


    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助