WPF MVVM: How to close a window

后端 未结 21 1764
后悔当初
后悔当初 2020-12-04 08:19

I have a Button that closes my window when it\'s clicked:


21条回答
  •  Happy的楠姐
    2020-12-04 08:56

    I've tried to resolve this issue in some generic, MVVM way, but I always find that I end up unnecessary complex logic. To achieve close behavior I have made an exception from the rule of no code behind and resorted to simply using good ol' events in code behind:

    XAML:

    Code behind:

    private void OnCloseClicked(object sender, EventArgs e)
    {
        Visibility = Visibility.Collapsed;
    }
    

    Although I wish this would be better supported using commands/MVVM, I simply think that there is no simpler and more clear solution than using events.

提交回复
热议问题