一、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>
代码效果如下:
代码效果
网友评论