첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ 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="UIElement 클래스 : GotFocus/LostFocus 이벤트를 사용해 키보드 포커스 획득/상실시 처리하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <Button Name="button1"
                Width="100"
                Height="30">
                테스트
            </Button>
            <Button Name="button2"
                Margin="0 10 0 0"
                Width="100"
                Height="30">
                테스트
            </Button>
        </StackPanel>
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.button1.GotFocus  += button_GotFocus;
            this.button1.LostFocus += button_LostFocus;

            this.button2.GotFocus  += button_GotFocus;
            this.button2.LostFocus += button_LostFocus;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 버튼 포커스 획득시 처리하기 - button_GotFocus(sender, e)

        /// <summary>
        /// 버튼 포커스 획득시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void button_GotFocus(object sender, RoutedEventArgs e)
        {
            Button button = e.Source as Button;

            button.Background = Brushes.Red;
        }

        #endregion
        #region 버튼 포커스 상실시 처리하기 - button_LostFocus(sender, e)

        /// <summary>
        /// 버튼 포커스 상실시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void button_LostFocus(object sender, RoutedEventArgs e)
        {
            Button button = e.Source as Button;

            button.Background = Brushes.White;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요