ソースコード
App.xaml
<Application x:Class="WpfApplication6.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> </Application>
MainWindow.xaml
<Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="100" Width="150"> <StackPanel> <Button>A</Button> <Button>B</Button> </StackPanel> </Window>
MainWindow.xaml.cs
using System.Windows; using System.Windows.Controls; namespace WpfApplication6 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); RoutedEventHandler handler = (s, e) => MessageBox.Show(string.Format("{0}がクリックされました。", ((Button) e.Source).Content)); this.AddHandler(Button.ClickEvent, handler); } } }