WPF 프로그래밍 - 2. C#, WPF HelloWorld(XAML기반)

Soonyoung Kim·2021년 8월 13일
0

WPF 프로그래밍

목록 보기
2/5

WPF HelloWorld(XAML기반) 실습

  • FILE -> NEW -> Project -> WPF 애플리케이션

  • 프로젝트명 : HelloWorld1

  • 솔루션 탐색기에서 MainWindow.xaml 파일 클릭

  • 왼쪽 바에서 도구상자 클릭

    • 공용 WPF 컨트롤
    • -> Lable, TextBox, Button, TextBlock 을 xaml파일로 드래그 하기
    • -> 드래그 후 F4를 눌러 속성 변경(Content 변경하여 속성명을 변경 가능)
    • -> 속성에서 번개모양 버튼(선택요소의 이벤트 처리기) 클릭
    • -> 필요한 이벤트를 선택 및 입력 후, 더블클릭을 하게 되면 cs파일에 이벤트 핸들러 메소드 생성
  • 수행 화면

    • TextBox에 입력 후, 버튼 클릭하면 다음의 코드 수행
      MessageBox.Show(textBox.Text+"님 안녕하세요.","Hello World");
    • textBlock_MouseLeftButtonUp 마우스 왼쪽 클릭 시, 다음의 코드 수행
      MessageBox.Show("Hi, there!", "Hello World", MessageBoxButton.OK, MessageBoxImage.Information);
  • xaml

    • .xaml : UI를 만드는 파일이다.
<Window x:Class="HelloWorld1.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:HelloWorld1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Label Content="Enter a Name?" HorizontalAlignment="Left" Margin="151,104,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.596,-1.501"/>
        <Button Content="Click me" HorizontalAlignment="Left" Margin="472,109,0,0" VerticalAlignment="Top" Click="Button_Click"/>
        <TextBlock HorizontalAlignment="Left" Margin="302,155,0,0" Text="Hello World i am Click" TextWrapping="Wrap" VerticalAlignment="Top" Width="179" MouseLeftButtonUp="TextBlock_MouseLeftButtonUp"/>
        <TextBox HorizontalAlignment="Left" Margin="316,110,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
    </Grid>
</Window>
  • xaml.cs
using System.Windows;
using System.Windows.Input;


namespace HelloWorld1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(textBox.Text+"님 안녕하세요.","Hello World");
        }

        private void textBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Hi, there!", "Hello World", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}

profile
Sin prisa, sin pausa.

0개의 댓글