c#/WPF

[WPF] MVVM 패턴 적용해보기 (기초, 초급, 쉬움)

Heeyeon Choi 2023. 4. 12. 11:38
728x90

ViewModels 폴더 생성 -> MainViewModel.cs 생성하기

- INotifyPropertychanged 를 상속받아야 함 : 값이 변경되면 전달하기 위해

- progressValue 값이 변한 것을 View에 나타낼것임

- INotifyPropertychanged  인터페이스 구현해주기

 public event PropertyChangedEventHandler PropertyChanged;
        // This method is called by the Set accessor of each property.
        // The CallerMemberName attribute that is applied to the optional propertyName
        // parameter causes the property name of the caller to be substituted as an argument.
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

Xaml에 progessbar 추가하기

Value 값에 Binding 해주기

Xaml.cs 에 코드 추가해주기

- MainViewModel 객체 생성 후, ProgressValue 값 변경

 

결과

버튼 누르기 전
버튼 누른 후

 

728x90