본문 바로가기
Programming

C# WPF/WinForms UI 프로그래밍

by 나무수피아는 지식의 가지를 뻗어가는 공간입니다. 2025. 12. 12.
반응형

WPF MVVM 패턴

MVVM(Model-View-ViewModel) 패턴은 WPF 애플리케이션에서 UI와 비즈니스 로직을 깔끔하게 분리할 수 있도록 돕습니다.

  • Model : 데이터와 비즈니스 로직 담당
  • View : XAML로 구성된 사용자 인터페이스
  • ViewModel : View와 Model 사이를 연결하고 데이터 바인딩을 지원

MVVM을 통해 코드의 테스트성과 유지보수성이 크게 향상됩니다.

XAML 기초

XAML(eXtensible Application Markup Language)은 UI를 선언적으로 작성하는 마크업 언어입니다.

XAML 예시

<Window x:Class="SampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Sample" Height="350" Width="525">
    <Grid>
        <Button Content="클릭" Width="100" Height="30" />
    </Grid>
</Window>
    

간단한 UI 요소(Button, TextBox 등)를 쉽게 배치할 수 있습니다.

바인딩, 커맨드, 리소스

데이터 바인딩

View와 ViewModel 간에 데이터를 연결하는 방법입니다.

<TextBox Text="{Binding UserName}" Width="200" />
    

UserName은 ViewModel의 속성입니다.

커맨드(Command)

버튼 클릭 같은 이벤트를 ViewModel에서 처리할 수 있게 합니다.

<Button Content="저장" Command="{Binding SaveCommand}" Width="100" />
    

ViewModel에 SaveCommand라는 ICommand 구현체를 두어 이벤트를 처리합니다.

리소스(Resource)

스타일, 템플릿, 브러시 등을 공통으로 관리할 수 있습니다.

<Window.Resources>
    <SolidColorBrush x:Key="PrimaryColor" Color="LightBlue" />
</Window.Resources>

<Button Content="Primary" Background="{StaticResource PrimaryColor}" />
    
반응형

'Programming' 카테고리의 다른 글

C# 예외 처리  (13) 2025.12.14
C# NET Core  (0) 2025.12.13
C# LINQ(Language Integrated Query)  (0) 2025.12.11
C# 델리게이트와 이벤트  (0) 2025.12.10
C# 컬렉션과 제네릭  (0) 2025.12.09