■ UIElement 클래스의 AddHandler 메소드를 사용해 이벤트 핸들러를 추가하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="UIElement 클래스 : AddHandler 메소드를 사용해 이벤트 핸들러 추가하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="{x:Type Button}"> <Setter Property="Margin" Value="5" /> <Setter Property="BorderThickness" Value="3" /> </Style> </Window.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Button Name="A" Grid.Row="0" Grid.Column="0" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> <Button Name="B" Grid.Row="0" Grid.Column="1" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> <Button Name="C" Grid.Row="0" Grid.Column="2" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> <Button Name="D" Grid.Row="1" Grid.Column="0" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> <Button Name="E" Grid.Row="1" Grid.Column="1" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> <Button Name="F" Grid.Row="1" Grid.Column="2" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> <Button Name="G" Grid.Row="2" Grid.Column="0" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> <Button Name="H" Grid.Row="2" Grid.Column="1" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> <Button Name="I" Grid.Row="2" Grid.Column="2" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
using System.Windows; using System.Windows.Controls; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); AddHandler(Button.ClickEvent, new RoutedEventHandler(button_Click)); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 버튼 클릭시 처리하기 - button_Click(sender, e) /// <summary> /// 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Click(object sender, RoutedEventArgs e) { MessageBox.Show($"{(e.Source as Button).Content} 버튼을 클릭했습니다."); } #endregion } } |