첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ FrameworkElement 클래스 : MoveFocus 메소드를 사용해 포커스 이동하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip



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

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요