简体   繁体   English

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

[英]WPF : How to override Loaded Event in a Custom Control

I would like to override the Loaded event in my Custom control but I don't know how to do it.我想覆盖自定义控件中的Loaded事件,但我不知道该怎么做。

I wanted to do like this example but knowing that base.OnLoaded does not exist I don't know how to do it我想做这个例子,但知道base.OnLoaded不存在我不知道该怎么做

https://stackoverflow.com/a/28326029/14338640 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.请参阅我为 TextBox 控件尝试的以下代码。

 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 .有关信息,我的自定义控件继承自UserControl

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.由于没有要覆盖的OnLoaded方法,您唯一的选择是附加事件处理程序。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM