Tom
Tom

Reputation: 6971

Add external event handler to WPF component in C#/ Xaml?

Assume we have a button declared in XML :

<button x:Class="A" Content="click me" Name="button" />

And in the accompanying class A we have

public partial class A {
   BHandler b = new BHandler();
}

and the class BHandler consits of:

public class BHandler {
   clickHandler(object sender, RoutedEventArgs){
      Console.WriteLine("button clicked!");
   }
}

My question is: Is it possible to register the event handler in b directly to the button, by something like:

Click="b.clickHandler"

rather than having the event handler in the partial C# class that accompanies the AXML?

Upvotes: 0

Views: 908

Answers (1)

Felice Pollano
Felice Pollano

Reputation: 33252

You can for example use some kind of MVVM frameworks as for example Caliburn Micro. With this one you can just have the function implemented in the view model and have it bound autoamtically by naming convention.

Upvotes: 1

Related Questions