WPF例4-查找触发路由事件的按钮

一、MainWindow中设置捕获ButtonBase.Click事件,触发Window_Click函数

MainWindow.xaml.cs文件代码

using System.Windows;
using System.Windows.Controls;

namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        //事件处理函数
        private void Window_Click(object sender, RoutedEventArgs e)
        {
            Button btn = e.OriginalSource as Button;
            MessageBox.Show(btn.Content as string + "被单击了!");
        }
    }
}

MainWindow.xaml文件代码

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" ButtonBase.Click="Window_Click">
    <Grid>
        <Button Content="按钮1" HorizontalAlignment="Left" Margin="353,87,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="按钮2" HorizontalAlignment="Left" Margin="353,144,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="按钮3" HorizontalAlignment="Left" Margin="353,203,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

代码效果如下:

代码效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,581评论 1 11
  • 事件流 JavaScript与HTML之间的交互是通过事件实现的。事件,就是文档或浏览器窗口中发生的一些特定的交互...
    DHFE阅读 860评论 0 3
  • 本节介绍各种常见的浏览器事件。 鼠标事件 鼠标事件指与鼠标相关的事件,主要有以下一些。 click 事件,dblc...
    许先生__阅读 2,551评论 0 4
  • JavaScript 程序采用了异步事件驱动编程模型。在这种程序设计风格下,当文档、浏览器、元素或与之相关的对象发...
    劼哥stone阅读 1,300评论 3 11
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,431评论 0 3