■ UIElement 클래스를 사용해 라우팅 이벤트를 추적하는 방법을 보여준다.
▶ 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 |
<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 클래스 : 라우팅 이벤트 추적하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Name="grid"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Button Name="button" HorizontalAlignment="Center" Margin="10" MinWidth="120" Padding="5"> <TextBlock Name="buttonTextBlock" Text="테스트" /> </Button> <TextBlock Name="headerTextBlock" Grid.Row="1" /> <ScrollViewer Grid.Row="2"> <StackPanel Name="outputStackPanel" /> </ScrollViewer> </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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 포맷 문자열 /// </summary> private const string FORMAT_STRING = "{0,-30} {1,-15} {2,-15} {3,-15}"; /// <summary> /// 마지막 시간 /// </summary> private DateTime lastTime; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); string headerString = string.Format ( FORMAT_STRING, "Routed Event", "sender", "Source", "OriginalSource" ); this.headerTextBlock.Inlines.Add(new Underline(new Run(headerString))); UIElement[] elementArray = { this, this.grid, this.button, this.buttonTextBlock }; foreach(UIElement element in elementArray) { // 키보드 element.PreviewKeyDown += element_EventHandler; element.PreviewKeyUp += element_EventHandler; element.PreviewTextInput += element_EventHandler; element.KeyDown += element_EventHandler; element.KeyUp += element_EventHandler; element.TextInput += element_EventHandler; // 마우스 element.MouseDown += element_EventHandler; element.MouseUp += element_EventHandler; element.PreviewMouseDown += element_EventHandler; element.PreviewMouseUp += element_EventHandler; // 스타일러스 element.StylusDown += element_EventHandler; element.StylusUp += element_EventHandler; element.PreviewStylusDown += element_EventHandler; element.PreviewStylusUp += element_EventHandler; element.AddHandler(Button.ClickEvent, new RoutedEventHandler(element_EventHandler)); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 엘리먼트 이벤트 핸들러 처리하기 - element_EventHandler(sender, e) /// <summary> /// 엘리먼트 이벤트 핸들러 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void element_EventHandler(object sender, RoutedEventArgs e) { DateTime currentTime = DateTime.Now; if(currentTime - this.lastTime > TimeSpan.FromMilliseconds(100)) { this.outputStackPanel.Children.Add(new TextBlock(new Run(" "))); } this.lastTime = currentTime; TextBlock textBlock = new TextBlock(); textBlock.Text = string.Format ( FORMAT_STRING, e.RoutedEvent.Name, GetTypeNameWithoutNamespace(sender), GetTypeNameWithoutNamespace(e.Source), GetTypeNameWithoutNamespace(e.OriginalSource) ); this.outputStackPanel.Children.Add(textBlock); (this.outputStackPanel.Parent as ScrollViewer).ScrollToBottom(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 네임스페이스 없는 타입명 구하기 - GetTypeNameWithoutNamespace(source) /// <summary> /// 네임스페이스 없는 타입명 구하기 /// </summary> /// <param name="source">소스 객체</param> /// <returns>네임스페이스 없는 타입명</returns> private string GetTypeNameWithoutNamespace(object source) { string[] itemArray = source.GetType().ToString().Split('.'); return itemArray[itemArray.Length - 1]; } #endregion } } |