■ UIElement 클래스의 MoveFocus 메소드를 사용해 포커스를 이동하는 방법을 보여준다.
▶ 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 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 |
<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 클래스 : MoveFocus 메소드를 사용해 포커스 이동하기" Focusable="False" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="{x:Type RadioButton}"> <Setter Property="Margin" Value="10" /> </Style> <Style TargetType="{x:Type Button}"> <Style.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="LemonChiffon" /> </Trigger> </Style.Triggers> <Setter Property="Margin" Value="10" /> <Setter Property="Width" Value="50" /> <Setter Property="Height" Value="50" /> <Setter Property="Background" Value="AliceBlue" /> </Style> <Style x:Key="BorderStyleKey" TargetType="{x:Type Border}"> <Setter Property="Margin" Value="10" /> <Setter Property="BorderBrush" Value="Black" /> <Setter Property="BorderThickness" Value="2" /> </Style> </Window.Resources> <Grid Background="Ivory" Focusable="False"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False"> <Grid.RowDefinitions> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Margin="5"> <Border Style="{StaticResource BorderStyleKey}" HorizontalAlignment="Center"> <StackPanel Margin="10" Focusable="False" RadioButton.Checked="radioButton_Checked"> <Label HorizontalAlignment="Center"> 포커스 이동 방향 </Label> <RadioButton Content="아래쪽" Tag="Down" Focusable="False" /> <RadioButton Content="왼쪽" Tag="Left" Focusable="False" /> <RadioButton Content="다음" Tag="Next" Focusable="False" /> <RadioButton Content="이전" Tag="Previous" Focusable="False" /> <RadioButton Content="오른쪽" Tag="Right" Focusable="False" IsChecked="True"/> <RadioButton Content="위쪽" Tag="Up" Focusable="False" /> </StackPanel> </Border> <Border Style="{StaticResource BorderStyleKey}" HorizontalAlignment="Center"> <StackPanel> <Button Name="predictButton" Width="140" Height="50" HorizontalAlignment="Center" Focusable="False"> 포커스 예측하기 </Button> <Button Name="moveButton" HorizontalAlignment="Center" Width="140" Height="50" Focusable="False"> 포커스 이동하기 </Button> </StackPanel> </Border> </StackPanel> <StackPanel Grid.Column="1"> <Label HorizontalAlignment="Center"> 엘리먼트 사이에서 포커스 이동하기 </Label> <Border Grid.Column="1" Style="{StaticResource BorderStyleKey}"> <StackPanel Orientation="Horizontal"> <StackPanel VerticalAlignment="Center" Orientation="Horizontal"> <Button Name="firstButton" Content="1" /> <Button Content="2" /> <Button Content="3" /> </StackPanel> <StackPanel VerticalAlignment="Center" Orientation="Vertical"> <Button Content="4" /> <Button Content="5" /> <Button Content="6" /> <Button Content="7" /> <Button Content="8" /> </StackPanel> <StackPanel VerticalAlignment="Center" Orientation="Horizontal"> <Button Content="9" /> <Button Content="10" /> <Button Content="11" /> </StackPanel> </StackPanel> </Border> </StackPanel> </Grid> </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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Input; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 포커스 탐색 방향 /// </summary> private FocusNavigationDirection focusNavigationDirection; /// <summary> /// 포커스 예측 여부 /// </summary> private bool focusPredicted = false; /// <summary> /// 예측 컨트롤 /// </summary> private Control predictedControl; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.predictButton.PreviewMouseDown += predictButton_PreviewMouseDown; this.predictButton.PreviewMouseUp += predictButton_PreviewMouseUp; this.moveButton.Click += moveButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { Keyboard.Focus(this.firstButton); } #endregion #region 라디오 버튼 체크시 처리하기 - radioButton_Checked(sender, e) /// <summary> /// 라디오 버튼 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void radioButton_Checked(object sender, RoutedEventArgs e) { RadioButton radioButton = e.Source as RadioButton; if(radioButton != null) { this.focusNavigationDirection = (FocusNavigationDirection)Enum.Parse ( typeof(FocusNavigationDirection), (string)radioButton.Tag ); } } #endregion #region 포커스 예측하기 버튼 프리뷰 마우스 DOWN 처리하기 - predictButton_PreviewMouseDown(sender, e) /// <summary> /// 포커스 예측하기 버튼 프리뷰 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void predictButton_PreviewMouseDown(object sender, RoutedEventArgs e) { DependencyObject predictionElement = null; UIElement focusedElement = Keyboard.FocusedElement as UIElement; if(focusedElement != null) { if ( (this.focusNavigationDirection == FocusNavigationDirection.Up ) || (this.focusNavigationDirection == FocusNavigationDirection.Down ) || (this.focusNavigationDirection == FocusNavigationDirection.Left ) || (this.focusNavigationDirection == FocusNavigationDirection.Right) ) { predictionElement = focusedElement.PredictFocus(this.focusNavigationDirection); Control control = predictionElement as Control; if(control != null) { control.Foreground = Brushes.DarkBlue; control.FontSize += 10; control.FontWeight = FontWeights.ExtraBold; this.focusPredicted = true; this.predictedControl = control; } } } } #endregion #region 포커스 예측하기 버튼 프리뷰 마우스 UP 처리하기 - predictButton_PreviewMouseUp(sender, e) /// <summary> /// 포커스 예측하기 버튼 프리뷰 마우스 UP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void predictButton_PreviewMouseUp(object sender, RoutedEventArgs e) { if(this.focusPredicted == true) { this.predictedControl.Foreground = Brushes.Black; this.predictedControl.FontSize -= 10; this.predictedControl.FontWeight = FontWeights.Normal; this.focusPredicted = false; } } #endregion #region 포커스 이동하기 버튼 클릭시 처리하기 - moveButton_Click(sender, e) /// <summary> /// 포커스 이동하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void moveButton_Click(object sender, RoutedEventArgs e) { FocusNavigationDirection focusNavigationDirection = this.focusNavigationDirection; TraversalRequest traversalRequest = new TraversalRequest(focusNavigationDirection); UIElement focusesElement = Keyboard.FocusedElement as UIElement; if(focusesElement != null) { focusesElement.MoveFocus(traversalRequest); } } #endregion } } |