WPF MVVM: How to close a window

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

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


21条回答
  •  佛祖请我去吃肉
    2020-12-04 08:45

    There is a useful behavior for this task which doesn't break MVVM, a Behavior, introduced with Expression Blend 3, to allow the View to hook into commands defined completely within the ViewModel.

    This behavior demonstrates a simple technique for allowing the ViewModel to manage the closing events of the View in a Model-View-ViewModel application.

    This allows you to hook up a behavior in your View (UserControl) which will provide control over the control's Window, allowing the ViewModel to control whether the window can be closed via standard ICommands.

    Using Behaviors to Allow the ViewModel to Manage View Lifetime in M-V-VM

    http://gallery.expression.microsoft.com/WindowCloseBehavior/

    Above link has been archived to http://code.msdn.microsoft.com/Window-Close-Attached-fef26a66#content

提交回复
热议问题