WPF (MVVM): Closing a view from Viewmodel?

后端 未结 11 1521
小鲜肉
小鲜肉 2020-12-12 16:23

Anybody come across a clever way of closing a view in a viewmodel using MVVM?

Maybe there is a way of using binding to signal the view (window) to close?

I w

相关标签:
11条回答
  • 2020-12-12 16:44

    I used to use the dialogcloser attached behavior, but i find the below solution easier where I can use it. The sample below takes an example of a close button on the window for simplicity.

    pass the window as the command parameter.

    in the button xaml for the view:

    CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
    

    in the command execute method in the view model:

    if (parameter is System.Windows.Window)
    {
        (parameter as System.Windows.Window).Close();
    }
    
    0 讨论(0)
  • 2020-12-12 16:49

    You can also do this using event. Though you need like 3 lines of codes in your view code behind (some MVVM purist don't like this);

    In your viewmodel, you create an event that the view can subscribe to:

        public event CloseEventHandler Closing;
        public delegate void CloseEventHandler();
        private void RaiseClose()
        {
            if (Closing != null)
                Closing();
        }
    

    In your, view you subscribe to the event after your initializecomponent method as below:

            public View
            {
               *//The event can be put in an interface to avoid direct dependence of the view on the viewmodel. So below becomes
                //ICloseView model = (ICloseView)this.DataContext;*
                ProgressWindowViewModel model = (ProgressWindowViewModel)this.DataContext;
                model.Closing += Model_Closing;
            }
            private void Model_Closing()
            {
                 this.Close();
            }
    

    You just simply call RaiseClose() when you are ready to close the View from the ViewModel.

    You can even use this method to send message to the view from the viewmodel.

    The event can be put in an interface to avoid direct dependence of the view on the viewmodel.

    0 讨论(0)
  • 2020-12-12 16:51

    http://adammills.wordpress.com/2009/07/01/window-close-from-xaml/

    <Style.Triggers> <DataTrigger Binding="{Binding CloseSignal}" Value="true"> <Setter Property="Behaviours:WindowCloseBehaviour.Close" Value="true" /> </DataTrigger> </Style>

    0 讨论(0)
  • 2020-12-12 16:51

    You can make a command that attaches to the window and when executed closes the window. Then you can bind that command to a property on your view model, and execute the command when you want to close the window.

    0 讨论(0)
  • 2020-12-12 16:51

    This answer shows another way to do this:

    How should the ViewModel close the form?

    It uses an attached property to bind the DialogResult window property to a ViewModel property. When setting the value of DialogResult to true or false, the view is closed.

    0 讨论(0)
  • 2020-12-12 16:52

    I would use an ApplicationController which instantiates the LoginViewModel and shows the LoginView. When the user proceeds with the login screen the ApplicationController closes the LoginView and shows the MainView with its MainViewModel.

    How this can be done is shown in the sample applications of the WPF Application Framework (WAF) project.

    0 讨论(0)
提交回复
热议问题