■ FrameworkElement 엘리먼트에서 XAML을 사용해 포커스를 설정하는 방법을 보여준다.
▶ FrameworkElementExtension.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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
using System; using System.Windows; namespace TestProject; /// <summary> /// 프레임워크 엘리먼트 확장 /// </summary> public static class FrameworkElementExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 포커스 여부 속성 - IsFocusedProperty /// <summary> /// 포커스 여부 속성 /// </summary> public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached ( "IsFocused", typeof(bool?), typeof(FrameworkElementExtension), new FrameworkPropertyMetadata(IsFocusedPropertyChangedCallback) { BindsTwoWayByDefault = true } ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 포커스 여부 구하기 - GetIsFocused(d) /// <summary> /// 포커스 여부 구하기 /// </summary> /// <param name="d">의존 객체</param> /// <returns>포커스 여부</returns> public static bool? GetIsFocused(DependencyObject d) { if(d == null) { throw new ArgumentNullException("d"); } return (bool?)d.GetValue(IsFocusedProperty); } #endregion #region 포커스 여부 설정하기 - SetIsFocused(d, value) /// <summary> /// 포커스 여부 설정하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="value">값</param> public static void SetIsFocused(DependencyObject d, bool? value) { if(d == null) { throw new ArgumentNullException("d"); } d.SetValue(IsFocusedProperty, value); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 프레임워크 엘리먼트 포커스 획득시 처리하기 - frameworkElement_GotFocus(sender, e) /// <summary> /// 프레임워크 엘리먼트 포커스 획득시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void frameworkElement_GotFocus(object sender, RoutedEventArgs e) { ((FrameworkElement)sender).SetValue(IsFocusedProperty, true); } #endregion #region 프레임워크 엘리먼트 포커스 상실시 처리하기 - frameworkElement_LostFocus(sender, e) /// <summary> /// 프레임워크 엘리먼트 포커스 상실시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void frameworkElement_LostFocus(object sender, RoutedEventArgs e) { ((FrameworkElement)sender).SetValue(IsFocusedProperty, false); } #endregion #region 프레임워크 엘리먼트 표시 여부 변경시 처리하기 - frameworkElement_IsVisibleChanged(sender, e) /// <summary> /// 프레임워크 엘리먼트 표시 여부 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void frameworkElement_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { FrameworkElement frameworkElement = sender as FrameworkElement; if(frameworkElement.IsVisible && (bool)((FrameworkElement)sender).GetValue(IsFocusedProperty)) { frameworkElement.IsVisibleChanged -= frameworkElement_IsVisibleChanged; frameworkElement.Focus(); } } #endregion #region 포커스 여부 속성 변경시 콜백 처리하기 - IsFocusedPropertyChangedCallback(d, e) /// <summary> /// 포커스 여부 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void IsFocusedPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { FrameworkElement frameworkElement = d as FrameworkElement; if(e.OldValue == null) { frameworkElement.GotFocus -= frameworkElement_GotFocus; frameworkElement.GotFocus += frameworkElement_GotFocus; frameworkElement.LostFocus -= frameworkElement_LostFocus; frameworkElement.LostFocus += frameworkElement_LostFocus; } if(!frameworkElement.IsVisible) { frameworkElement.IsVisibleChanged -= frameworkElement_IsVisibleChanged; frameworkElement.IsVisibleChanged += frameworkElement_IsVisibleChanged; } if((bool)e.NewValue) { frameworkElement.Focus(); } } #endregion } |
▶ 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 |
<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="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBox Width="200" Height="25" VerticalContentAlignment="Center" /> <TextBox Margin="0 10 0 0" Width="200" Height="25" local:FrameworkElementExtension.IsFocused="True" VerticalContentAlignment="Center" /> </StackPanel> </Window> |