본문 바로가기
C#/C# WPF

Close Window in ViewModel

by doublerabbits 2022. 10. 23.

MVVM 패턴에서 ViewModel 이 Window 를 직접 접근하는 것은 권장하지 않는다.

 

방법 1.

Application.Current.MainWindow.Close()

 

 

방법 2.

ViewModel Code

public class ViewModel
{
    public Action CloseAction { get; set; }

    private void CloseCommandFunction()
    {
        CloseAction();
    }
}

 View Code(XAML)

public partial class DialogWindow : Window
{
    public DialogWindow()
    {
        ViewModel vm = new ViewModel();
        this.DataContext = vm;

        vm.CloseAction = new Action(() => this.Close());
    }
}

 

 

 

 

 

 

https://stackoverflow.com/questions/11945821/implementing-close-window-command-with-mvvm

 

Implementing "close window" command with MVVM

So my first attempt did everything out of the code behind, and now I'm trying to refactor my code to use the MVVM pattern, following the guidance of the MVVM in the box information. I've created a

stackoverflow.com

 

'C# > C# WPF' 카테고리의 다른 글

Button click parameter (code behind)  (0) 2023.06.01
Xaml 특수문자  (0) 2023.06.01
Trigger  (0) 2022.10.23
이벤트 라우팅(Event Routing)  (0) 2022.09.30
UserControl WindowStyle  (0) 2022.09.27