WPF 간단한 데스크톱 앱 구현

WPF와 MVVM 패턴을 학습하며 구현한 카운터 애플리케이션이다.

데이터 바인딩, 명령 처리, 레이아웃 설계 등 WPF의 핵심 요소를 포함하고 있다.

구현 과정을 정리했다.

뷰모델: 상태 및 행위 정의

카운터의 값과 동작을 뷰모델에 정의했다.

UI와 분리된 독립적 로직 영역이다.

public class CounterViewModel : Notifier
{
    private int _count;
    public int Count
    {
        get => _count;
        set { _count = value; OnPropertyChanged(); } // 값 변경 시 UI 통지
    }

    // 명령(Command)을 통한 행위 정의
    public ICommand IncreaseCommand => new RelayCommand(() => Count++);
    public ICommand DecreaseCommand => new RelayCommand(() => Count--);
}

XAML: UI 구성

뷰모델의 데이터와 명령을 UI 요소에 연결했다.

비하인드 코드 없이 화면을 구성했다.

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
    <Button Content="-" Command="{Binding DecreaseCommand}" />
    <TextBlock Text="{Binding Count}" FontSize="30" Margin="20,0" />
    <Button Content="+" Command="{Binding IncreaseCommand}" />
</StackPanel>

구현 결과 분석

  • 데이터 바인딩의 효율성: Count 값 변경 시 UI가 자동으로 갱신된다.
    수동 갱신 로직이 불필요하다.

  • 로직 독립성: 버튼 클릭 시 실행될 코드가 뷰모델에 분리되어 있어 기능 확장 및 테스트에 유리하다.

UI와 로직의 통신 원리를 파악한 프로젝트였다.

이 기초를 바탕으로 복잡한 데스크톱 애플리케이션 개발이 가능하는 것 같다.

Author avatar

웨이호프

WordPress creator and blogger.

View all posts