在WPF中处理用户控件事件

编程入门 行业动态 更新时间:2024-10-11 19:14:56
本文介绍了在WPF中处理用户控件事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好,我有一个简单的UserControl和一个Button。如何为我的控件定义自定义单击事件,以便在应用程序中使用此用户控件时,单击按钮,引发自定义事件?

Hi everybody, I have a simple UserControl and a Button on it. How can i define a custom click event for my control so that when i use this user contol in the application, clicking the button, raise my custom event?

推荐答案

I我找到了一个解决方案,不确定它是最好的答案。 1)用户控制Xaml: I've found a solution, not sure it's the best answer though. 1) User control Xaml: <MyButton Click="OnButtonClick">

2)用户控制代码背后:

2) User control code behind:

public event RoutedEventHandler CustomClick; private void OnButtonClick( object sender, RoutedEventArgs e ) { if( CustomClick != null ) { CustomClick(this, new RoutedEventArgs()); } }

3)主窗口Xaml:

3) Main window Xaml:

<MyCustomControl CustomClick = "OnCustomButtonClick">

4)背后的主窗口代码:

4) Main window code behind:

private void OnCustomButtonClick(object sender, RoutedEventArgs e) { //do something ... }

更多推荐

在WPF中处理用户控件事件

本文发布于:2023-11-12 13:18:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   事件   用户   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!