-1

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.

Xaalek
  • 168
  • 1
  • 9
  • 1
    Attach a Loaded event handler in the control's constructor. – Clemens May 11 '22 at 09:41
  • @Clemens `Handles MyBase.Loaded` ? Let's say it works. How do I call `MyBase.OnLoad` since this function doesn't exist ? – Xaalek May 11 '22 at 09:45
  • 1
    No need to call any base class method. The base class method calls your event handler. Take a look at the online documentation of the Loaded event. – Clemens May 11 '22 at 09:48

1 Answers1

3

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)
{
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • 1
    It is necessary, otherwise your UserControl would not load its XAML. Note that you could alternatively also attach the handler in XAML by ``. – Clemens May 11 '22 at 12:23