-2

I have a question and it is How can I define a new custom event for my defined control?

I want to define click event handler for my control and it fired up when user clicked on button. You can download my sample form here: http://s000.tinyupload.com/index.php?file_id=16349642872382657459

Mirror: https://www.mediafire.com/file/fhkix3wdmpgl806/WpfApp1.zip

I'm waiting for your advice.

tnx

Shahryar
  • 39
  • 1
  • 3
  • 10
  • You can refer this https://stackoverflow.com/questions/13447940/how-to-create-user-define-new-event-for-user-control-in-wpf-one-small-example – Smirti Oct 04 '17 at 05:49

1 Answers1

0

I've found my answer.

Here is my solution:

We should put following code to CustomControl.cs file.

public override void OnApplyTemplate()
{
    var btnButton = GetTemplateChild("btnButton") as Button;
    if (btnButton != null)
        btnButton.Click += ButtonClick;
    base.OnApplyTemplate();
}
public event RoutedEventHandler ButtonClick;
private void OnButtonClick(object sender, RoutedEventArgs e) { }

and done. now we have a custom event for our control. too easy ;)

Shahryar
  • 39
  • 1
  • 3
  • 10
  • A general comment: in such a scenario it may be more useful for you to expose ICommand in CustomControl. That way you can directly bind a Command from your viewmodel, no need to use an event to command helpers. – Maciek Świszczowski Oct 04 '17 at 10:09