Implementing “close window” command with MVVM

后端 未结 12 1705
既然无缘
既然无缘 2020-12-05 09:31

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 inf

12条回答
  •  醉酒成梦
    2020-12-05 10:05

    using MVVM-light toolkit:

    In the ViewModel:

     public void notifyWindowToClose()
    {
        Messenger.Default.Send(
            new NotificationMessage(this, "CloseWindowsBoundToMe")
        );
    }
    

    And in the View:

     Messenger.Default.Register(this, (nm) =>
    {
        if (nm.Notification == "CloseWindowsBoundToMe")
        {
            if (nm.Sender == this.DataContext)
                this.Close();
        }
    });
    

提交回复
热议问题