[C# WPF] WPF Button 컨트롤 사용방법 (Button 선언, Click 이벤트)

안녕하세요.

 

오늘은 C# WPF 에서 Button 컨트롤 사용하는 방법에 대해서 알려 드리려고 합니다.

 

어떤 UI 도구를 사용하여 프로그램을 개발할 때 Button 컨트롤은 없어서는 안될 가장 중요하면서도 기초적인 컨트롤이라고 생각을 합니다.

 

그렇기 때문에 오늘은 WPF에서 Button 컨트롤 간단하게 사용하는 방법과 Button 컨트롤 Click 이벤트까지 선언하는 방법을 보여 드리도록 하겠습니다.

 

먼저, 아래와 같이 WPF 빈 윈도우를 하나 생성해 주시고, Button 컨트롤 3개를 배치해 주시기 바랍니다.

빈 WPF 생성 및 Button 컨트롤 배치

위와 같이 Button 컨트롤 3개를 배치해 주시기 바랍니다.

 

그리고 아래 예제 코드를 작성해 주시기 바랍니다.

 

Xaml 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<Window x:Class="ButtonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ButtonTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="250" Width="400">
    <Grid>
        <Button x:Name="uiBtn_Button1" Content="Button1" HorizontalAlignment="Left" 
                Margin="31,98,0,0" VerticalAlignment="Top" Height="51" Width="79"
                Click="uiBtn_Button_Click"/>
        <Button x:Name="uiBtn_Button2" Content="Button2" HorizontalAlignment="Center" 
                Margin="0,98,0,0" VerticalAlignment="Top" Height="51" Width="79"
                Click="uiBtn_Button_Click"/>
        <Button x:Name="uiBtn_Button3" Content="Button3" HorizontalAlignment="Left"
                Margin="286,98,0,0" VerticalAlignment="Top" Height="51" Width="79"
                Click="uiBtn_Button_Click"/>
    </Grid>
</Window>
 
cs

 

Main.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace ButtonTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// Button 컨트롤 클릭 이벤트 핸들러
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiBtn_Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            MessageBox.Show($"{btn.Name} 클릭 되었습니다.");
        }
    }
}
 
cs

 

실행 결과

위와 같이 각각의 버튼을 클릭하게 되면, 해당 버튼의 Name에 맞게 MessageBox가 호출되어 메시지가 출력되는 것을 확인하실 수 있습니다.

 

이로써, 오늘은 간단히 WPF에서 Button 컨트롤 사용방법에 대해서 알아 보았습니다.

 

다음 포스팅에서는 Button 컨트롤을 조금 더 다양하게 사용하는 방법에 대해서 알려 드리도록 하겠습니다.

 

감사합니다.^^

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY