美文网首页
WPF例4-查找触发路由事件的按钮

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

作者: quchangTJU | 来源:发表于2020-01-27 16:11 被阅读0次

    一、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>
    

    代码效果如下:

    代码效果

    相关文章

      网友评论

          本文标题:WPF例4-查找触发路由事件的按钮

          本文链接:https://www.haomeiwen.com/subject/knlethtx.html