在wpf中为自定义资源样式控件定义新的事件处理程序

时间:2017-10-04 04:58:55

标签: c# wpf

我有一个问题,它是如何为我定义的控件定义新的自定义事件?

我想为我的控件定义click事件处理程序,当用户单击按钮时它会启动。您可以在此处下载我的示例表单:http://s000.tinyupload.com/index.php?file_id=16349642872382657459

镜像:https://www.mediafire.com/file/fhkix3wdmpgl806/WpfApp1.zip

我在等你的建议。

TNX

1 个答案:

答案 0 :(得分:0)

我找到了答案。

这是我的解决方案:

我们应该将以下代码放到CustomControl.cs文件中。

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) { }

并完成了。现在我们有一个自定义事件供我们控制。太容易了;)