[C#/WPF] EventManager 클래스 : RegisterClassHandler 정적 메소드를 사용해 라우팅 이벤트 오버라이딩하기
C#/WPF 2020. 12. 1. 23:30728x90
728x170
▶ TestButton.cs
using System.Windows;
using System.Windows.Controls;
namespace TestProject
{
/// <summary>
/// 테스트 버튼
/// </summary>
public class TestButton : Button
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Static
#region 생성자 - TestButton()
/// <summary>
/// 생성자
/// </summary>
static TestButton()
{
EventManager.RegisterClassHandler
(
typeof(TestButton),
ClickEvent,
new RoutedEventHandler(button_Click)
);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 버튼 클릭시 처리하기 - button_Click(sender, e)
/// <summary>
/// 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private static void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("button_Click");
// 주석을 제거하면 하위 클래스의 클래스 핸들러만 응답한다.
e.Handled = true;
}
#endregion
}
}
728x90
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestProject"
Width="800"
Height="600"
Title="EventManager 클래스 : RegisterClassHandler 정적 메소드를 사용해 라우팅 이벤트 오버라이딩하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<local:TestButton x:Name="testButton"
Width="100"
Height="30"
Content="테스트" />
</Grid>
</Window>
300x250
▶ MainWindow.xaml.cs
using System.Windows;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
this.testButton.Click += testButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 테스트 버튼 클릭시 처리하기 - testButton_Click(sender, e)
/// <summary>
/// 테스트 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void testButton_Click(object sender, RoutedEventArgs e)
{
// 실행되지 않는다.
MessageBox.Show("testButton_Click");
}
#endregion
}
}
728x90
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Viewport2DVisual3D 엘리먼트 : 곡선 형태의 ScrollBar 사용하기 (0) | 2020.12.05 |
---|---|
[C#/WPF] Imaging 클래스 : CreateBitmapSourceFromHBitmap 정적 메소드를 사용해 아이콘에서 비트맵 소스 구하기 (0) | 2020.12.05 |
[C#/WPF] Imaging 클래스 : CreateBitmapSourceFromHBitmap 정적 메소드를 사용해 비트맵에서 비트맵 소스 구하기 (0) | 2020.12.05 |
[C#/WPF] TranslateTransform 클래스 사용하기 (0) | 2020.12.05 |
[C#/WPF] SkewTransform 클래스 사용하기 (0) | 2020.12.05 |
[C#/WPF] ScaleTransform 클래스 사용하기 (0) | 2020.11.24 |
[C#/WPF] RotateTransform 클래스 사용하기 (0) | 2020.11.24 |
[C#/WPF] 확대/축소/이동 가능한 캔버스 만들기 (0) | 2020.11.15 |
[C#/WPF] DispatcherTimer 클래스 : 원 형태으로 이동시키기 (0) | 2020.11.15 |
[C#/WPF] DispatcherTimer 클래스 : 파동 형태로 이동시키기 (0) | 2020.11.15 |