■ FrameworkElement 클래스 : MoveFocus 메소드를 사용해 포커스 이동하기
------------------------------------------------------------------------------------------------------------------------
▶ 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" Width="800" Height="600" Title="FrameworkElement 클래스 : MoveFocus 메소드를 사용해 포커스 이동하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="{x:Type TextBox}"> <Setter Property="Margin" Value="10" /> <Setter Property="Padding" Value="5" /> <Setter Property="VerticalContentAlignment" Value="Center" /> </Style> <Style TargetType="{x:Type Button}"> <Setter Property="Margin" Value="10" /> <Setter Property="Padding" Value="5" /> <Setter Property="Focusable" Value="False" /> </Style> </Window.Resources> <Grid Name="grid"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="20" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBox Name="textBox1" Grid.Row="0" Grid.Column="0" Text="텍스트 1" /> <TextBox Name="textBox2" Grid.Row="0" Grid.Column="1" Text="텍스트 2" /> <TextBox Name="textBox3" Grid.Row="0" Grid.Column="2" Text="텍스트 3" /> <TextBox Name="textBox4" Grid.Row="1" Grid.Column="0" Text="텍스트 4" /> <TextBox Name="textBox5" Grid.Row="1" Grid.Column="1" Text="텍스트 5" /> <TextBox Name="textBox6" Grid.Row="1" Grid.Column="2" Text="텍스트 6" /> <Button Name="previousButton" Grid.Row="3" Grid.Column="0" Content="이전" /> <Button Name="nextButton" Grid.Row="3" Grid.Column="2" Content="다음" /> </Grid> </Grid> </Window>
|
▶ MainWindow.xaml.cs
using System.Windows; using System.Windows.Input;
namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent();
this.previousButton.Click += previousButton_Click; this.nextButton.Click += nextButton_Click;
this.textBox1.Focus(); }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private
#region 이전 버튼 클릭시 처리하기 - previousButton_Click(sender, e)
/// <summary> /// 이전 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void previousButton_Click(object sender, RoutedEventArgs e) { IInputElement inputElement = FocusManager.GetFocusedElement(this);
FrameworkElement frameworkElement = inputElement as FrameworkElement;
frameworkElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous)); }
#endregion #region 다음 버튼 클릭시 처리하기 - nextButton_Click(sender, e)
/// <summary> /// 다음 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void nextButton_Click(object sender, RoutedEventArgs e) { IInputElement inputElement = FocusManager.GetFocusedElement(this);
FrameworkElement frameworkElement = inputElement as FrameworkElement;
frameworkElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); }
#endregion } }
|
------------------------------------------------------------------------------------------------------------------------
'C# > WPF' 카테고리의 다른 글
[C#/WPF] ElasticEase 엘리먼트 사용하기 (0) | 2020.08.09 |
---|---|
[C#/WPF] Slider 엘리먼트 사용하기 (0) | 2020.08.09 |
[C#/WPF] Polygon 클래스 : 다각형 그리기/편집하기 (0) | 2020.08.09 |
[C#/WPF] Keyboard 클래스 : FocusedElement 정적 속성을 사용해 포커스 엘리먼트 구하기 (0) | 2020.08.09 |
[C#/WPF] FocusManager 클래스 : GetFocusedElement 정적 메소드를 사용해 포커스 엘리먼트 구하기 (0) | 2020.08.09 |
[C#/WPF] FrameworkElement 클래스 : MoveFocus 메소드를 사용해 포커스 이동하기 (0) | 2020.08.09 |
[C#/WPF] Button 엘리먼트 : Style 속성 사용하기 (0) | 2020.08.09 |
[C#/WPF] Cursor 클래스 : 리소스 커서 로드하기 (0) | 2020.08.09 |
[C#/WPF] Mouse 클래스 : OverrideCursor 정적 속성을 사용해 커서 설정하기 (0) | 2020.08.09 |
[C#/WPF] Clipboard 클래스 : GetText 정적 메소드를 사용해 HTML 구하기 (0) | 2020.08.08 |
[C#/WPF] UIElement 클래스 : DragEnter/GiveFeedback/DragOver/Drop/DragLeave 이벤트를 사용해 드래그하기 (0) | 2020.08.08 |
댓글을 달아 주세요