WPF:如何在自定义控件中覆盖已加载的事件

发布于 2025-01-28 01:35:58 字数 632 浏览 4 评论 0原文

我想在自定义控件中覆盖已加载的事件,但我不知道该怎么做。

我想做这个示例,但是知道base.onloaded不存在我不知道该怎么做

https://stackoverflow.com/a/28326029/14338640

您可以创建自定义控件并覆盖事件。参考 下面我尝试过用于文本框控件的代码。

  class textboxex:textbox {
    受保护的覆盖void ontouchup(system.windows.input.toucheventargs e)
    {
        base.ontouchup(e);
    }

    受保护的替代void ontouchdown(system.windows.input.toucheventargs e)
    {
       base.ontouchdown(e);
    } 
} 
 

有关信息,我的自定义控件从usercontrol继承。

I would like to override the Loaded event in my Custom control but I don't know how to do it.

I wanted to do like this example but knowing that base.OnLoaded does not exist I don't know how to do it

https://stackoverflow.com/a/28326029/14338640

You can create a custom control and override the events. refer the
below code i tried for TextBox control.

class TextBoxEx : TextBox {
    protected override void OnTouchUp(System.Windows.Input.TouchEventArgs e)
    {
        base.OnTouchUp(e);
    }

    protected override void OnTouchDown(System.Windows.Input.TouchEventArgs e)
    {
       base.OnTouchDown(e);
    } 
} 

For information my custom control inherits from UserControl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2025-02-04 01:35:58

您不会“覆盖事件”。您覆盖方法或附加事件处理程序。

由于没有on Loaded替代方法,因此您唯一的选择是附加事件处理程序。

public YourControl()
{
    InitializeComponent();
    Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
}

You do not "override an event". You override a method or attach an event handler.

Since there is no OnLoaded method to override, your only option is to attach an event handler.

public YourControl()
{
    InitializeComponent();
    Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文