开发者

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

开发者 https://www.devze.com 2023-03-15 01:41 出处:网络
Assume we have a button declared in XML : <button x:Class=\"A\" Content=\"click me\" Name=\"button\" />

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 h开发者_开发知识库andler 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号