首页 文章

使用MVVM处理WPF中的对话框

提问于
浏览
220

在WPF的MVVM模式中,处理对话框是更复杂的操作之一 . 由于您的视图模型对视图一无所知,因此对话通信可能很有趣 . 我可以公开一个ICommand,当视图调用它时,会出现一个对话框 .

有没有人知道处理对话结果的好方法?我说的是关于Windows对话框,比如MessageBox .

我们这样做的方法之一是在视图模型上有一个事件,当需要对话框时视图会订阅 .

public event EventHandler<MyDeleteArgs> RequiresDeleteDialog;

这没关系,但这意味着视图需要代码,这是我想远离的东西 .

23 回答

  • 23

    我建议放弃1990年代的模态对话框,而是将控件实现为覆盖(画布绝对定位),其中可见性与VM中的布尔值相关联 . 更接近ajax类型控件 .

    这非常有用:

    <BooleanToVisibilityConverter x:Key="booltoVis" />
    

    如:

    <my:ErrorControl Visibility="{Binding Path=ThereWasAnError, Mode=TwoWay, Converter={StaticResource booltoVis}, UpdateSourceTrigger=PropertyChanged}"/>
    

    这是我如何实现一个用户控件 . 单击“x”会关闭usercontrol代码后面的一行代码中的控件 . (因为我在一个dll中的.exe和ViewModels中有我的视图,所以对于操作UI的代码我并不感到难过 . )

    Wpf dialog

  • 1

    你应该使用调解员 . Mediator是一种常见的设计模式,在其某些实现中也称为Messenger . 它是Register / Notify类型的范例,使您的ViewModel和Views能够通过低耦合的消息传导机制进行通信 .

    您应该查看google WPF Disciples组,然后只搜索Mediator . 你会对答案感到满意......

    但是你可以从这开始:

    http://joshsmithonwpf.wordpress.com/2009/04/06/a-mediator-prototype-for-wpf-apps/

    请享用 !

    编辑:您可以在此处查看MVVM Light Toolkit对此问题的答案:

    http://mvvmlight.codeplex.com/Thread/View.aspx?ThreadId=209338

  • 0

    一个好的MVVM对话框应该:

    • 仅使用XAML声明 .

    • 从数据绑定中获取所有这些行为 .

    不幸的是,WPF不提供这些功能 . 显示对话框需要对ShowDialog()进行代码隐藏调用 . 支持对话框的Window类不能在XAML中声明,因此不能轻易地将数据绑定到DataContext .

    为了解决这个问题,我编写了一个XAML存根控件,它位于逻辑树中,并将数据绑定中继到一个Window,并处理显示和隐藏对话框 . 你可以在这里找到它:http://www.codeproject.com/KB/WPF/XAMLDialog.aspx

    它只是简单地使用,并且不需要对ViewModel进行任何奇怪的更改,也不需要事件或消息 . 基本调用如下所示:

    <dialog:Dialog Content="{Binding Path=DialogViewModel}" Showing="True" />
    

    您可能想要添加一个设置显示的样式 . 我在文章中解释它 . 我希望这可以帮助你 .

  • -1

    我使用this方法与MVVM进行对话 .

    我现在要做的就是从我的视图模型中调用以下内容 .

    var result = this.uiDialogService.ShowDialog("Dialogwindow title goes here", dialogwindowVM);
    
  • 3

    我目前的解决方案解决了您提到的大部分问题,但它完全从平台特定事物中抽象出来,可以重复使用 . 此外,我没有使用代码隐藏仅与实现ICommand的DelegateCommands绑定 . Dialog基本上是一个View - 一个单独的控件,它有自己的ViewModel,它从主屏幕的ViewModel显示,但是通过DelagateCommand绑定从UI触发 .

    在这里查看完整的Silverlight 4解决方案Modal dialogs with MVVM and Silverlight 4

  • 28

    在学习(仍在学习)MVVM时,我真的很挣这个概念 . 我决定了什么,以及我认为其他人已经决定但我不清楚的是:

    我最初的想法是不应该允许ViewModel直接调用对话框,因为它没有业务决定对话框应该如何显示 . 因此我开始考虑如何传递消息,就像我在MVP中那样(即View.ShowSaveFileDialog()) . 但是,我认为这是错误的做法 .

    ViewModel可以直接调用对话框 . 但是,当您测试ViewModel时,这意味着对话框将在测试期间弹出,或者一起失败(从未真正尝试过) .

    因此,需要进行的是测试是使用对话框的“测试”版本 . 这意味着,对于您所拥有的对话框,您需要创建一个接口,并模拟对话框响应或创建一个具有默认行为的测试模拟 .

    您应该已经使用某种服务定位器或IoC,您可以根据上下文为您提供正确的版本 .

    使用这种方法,您的ViewModel仍然是可测试的,并且根据您模拟对话框的方式,您可以控制行为 .

    希望这可以帮助 .

  • 51

    花了一些时间后,我终于提出了以下解决方案 . 这种方法的一些关键优势是:

    • 实现MVVM Light自己的 IDialogService .

    • 查看没有't need to add MVVM Light'参考 .

    • VM甚至不需要 PresentationFramework 参考 .

    • 使用MVVM Light自己的Messenger通道,因此表示层和VM层是分离的 .

    • 支持具有返回值的对话框,例如是/否问题或确定/取消情况 .

    • 支持自定义对话框 .

    这是 IDialogService 的实现(进入 ViewModel 项目):

    using System;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace VM
    {
      public enum MessageBoxButtonVM
      {
        OK,
        OKCancel,
        YesNo
      }
    
      public enum MessageBoxImageVM
      {
        None,
        Information,
        Question,
        Error
      }
    
      public class MessageBoxArgs
      {
        public MessageBoxButtonVM Buttons { get; set; }
        public MessageBoxImageVM Icon { get; set; }
        public string Title { get; set; }
        public string Message { get; set; }
      }
    
      //For custom dialogs that return a value
      public class MessageBoxNotificationWithAction<T>
      {
        private readonly Action<T> _callback;
    
        public MessageBoxArgs Notification { get; set; }
    
        public MessageBoxNotificationWithAction(MessageBoxArgs notification, Action<T> callback)
        {
          Notification = notification;
    
          CheckCallback(callback);
          _callback = callback;
        }
    
        public virtual void Execute(T argument)
        {
          _callback.Invoke(argument);
        }
    
        private static void CheckCallback(Delegate callback)
        {
          if (callback == null)
          {
            throw new ArgumentNullException(nameof(callback), "Callback must not be null");
          }
        }
      }
    
      /// <summary>
      /// Provides an implementation-agnostic way of communicating with the user through dialog boxes. Clients must register for communication messages using
      /// MVVM Light messaging system.
      /// </summary>
      public class DialogService : GalaSoft.MvvmLight.Views.IDialogService
      {
        private static GalaSoft.MvvmLight.Messaging.IMessenger Messenger = GalaSoft.MvvmLight.Messaging.Messenger.Default;
    
        private string _ProductName = "";
    
        public string ProductName
        {
          get
          {
            if (_ProductName == "")
            {
              //The following statement returns the Title attribute of the current assembly, as defined in project properties (Assembly Information dialog).
              var TitleAttrib = System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributesData().First(x => x.AttributeType.Name == "AssemblyTitleAttribute");
    
              if (TitleAttrib != null)
              {
                _ProductName = TitleAttrib.ConstructorArguments[0].Value.ToString();
              }
              else
              {
                _ProductName = "Default Application Name";
              }
            }
    
            return _ProductName;
          }
        }
    
        public Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback)
        {
          return ShowError(error.Message, title, buttonText, afterHideCallback);
        }
    
        public Task ShowMessage(string message, string title)
        {
          return Task.Run(() => MessengerSend(message, title, MessageBoxButtonVM.OK, MessageBoxImageVM.Error));
        }
    
        public Task ShowError(string message, string title, string buttonText, Action afterHideCallback)
        {
          return Task.Run(() =>
          {
            MessengerSend(message, title, MessageBoxButtonVM.OK, MessageBoxImageVM.Error);
            afterHideCallback?.Invoke();
          });
        }
    
        public Task ShowMessage(string message, string title, string buttonText, Action afterHideCallback)
        {
          return Task.Run(() =>
          {
            MessengerSend(message, title);
            afterHideCallback?.Invoke();
          });
        }
    
        public Task<bool> ShowMessage(string message, string title, string buttonConfirmText, string buttonCancelText, Action<bool> afterHideCallback)
        {
          if ((buttonConfirmText == "OK" && buttonCancelText == "Cancel") ||
            (buttonConfirmText == "Yes" && buttonCancelText == "No"))
          {
            return Task.Run<bool>(() =>
            {
              MessageBoxButtonVM btn;
              if (buttonConfirmText == "OK")
                btn = MessageBoxButtonVM.OKCancel;
              else
                btn = MessageBoxButtonVM.YesNo;
    
    
              bool Response = false;
              Messenger.Send(new MessageBoxNotificationWithAction<bool>(
                                                          new MessageBoxArgs()
                                                          {
                                                            Buttons = btn,
                                                            Icon = MessageBoxImageVM.Question,
                                                            Title = (string.IsNullOrEmpty(title) ? _ProductName : title),
                                                            Message = message
                                                          },
                                                          (result) => Response = result
                                                            ));
    
              afterHideCallback?.Invoke(Response);
    
              return Response;
            });
          }
          else
            throw new ArgumentException($"{nameof(buttonConfirmText)} and {nameof(buttonCancelText)} must either be OK/Cancel or Yes/No.");
        }
    
        /// <summary>
        /// For debugging purpose only
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public Task ShowMessageBox(string message, string title) => ShowMessage(message, title);
    
        private void MessengerSend(string msg, string title = "", MessageBoxButtonVM btn = MessageBoxButtonVM.OK, MessageBoxImageVM icon = MessageBoxImageVM.Information)
        {
          Messenger.Send(new MessageBoxArgs()
          {
            Buttons = MessageBoxButtonVM.OK,
            Icon = MessageBoxImageVM.Information,
            Title = (string.IsNullOrEmpty(title) ? _ProductName : title),
            Message = msg
          });
        }
      }
    }
    

    这是表示层(进入 View 项目)

    using System.Windows;
    using VM;
    
    namespace View
    {
      class DialogPresenter
      {
        private Window _Parent;
    
        public DialogPresenter()
        {
          //For simple information boxes
          GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<MessageBoxArgs>(this, (arg) => ShowDialog(arg));
    
          //For Yes/No or OK/Cancel dialog boxes.
          GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<MessageBoxNotificationWithAction<bool>>(this, (arg) => arg.Execute(ShowDialog(arg.Notification)));
    
          //For notifications that require a string response (such as Manual Timeslot Description)
          GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<MessageBoxNotificationWithAction<string>>(this,
            (arg) => arg.Execute(ShowStringInputDialog(arg.Notification.Title, arg.Notification.Message)));
        }
    
        private bool ShowDialog(MessageBoxArgs arg)
        {
          MessageBoxButton btn = MessageBoxButton.OK;
          MessageBoxImage ico = MessageBoxImage.None;
    
          switch (arg.Buttons)
          {
            case MessageBoxButtonVM.OK: btn = MessageBoxButton.OK; break;
            case MessageBoxButtonVM.OKCancel: btn = MessageBoxButton.OKCancel; break;
            case MessageBoxButtonVM.YesNo: btn = MessageBoxButton.YesNo; break;
          }
    
          switch (arg.Icon)
          {
            case MessageBoxImageVM.Error: ico = MessageBoxImage.Error; break;
            case MessageBoxImageVM.Information: ico = MessageBoxImage.Information; break;
            case MessageBoxImageVM.None: ico = MessageBoxImage.None; break;
            case MessageBoxImageVM.Question: ico = MessageBoxImage.Question; break;
          }
    
          bool Result = false;
          _Parent.Dispatcher.Invoke(() =>
          {
            var Res = MessageBox.Show(arg.Message, arg.Title, btn, ico);
            Result = (Res == MessageBoxResult.OK || Res == MessageBoxResult.Yes);
          });
    
          return Result;
        }
    
        private string ShowStringInputDialog(string title, string description, string value = "", int maxLength = 100)
        {
          string Result = null;
    
          _Parent.Dispatcher.Invoke(() =>
          {
            //InputBox is a WPF Window I created for taking simple
            //string values from the user. This also shows that you can
            //any custom dialog using this approach.
    
            InputBox input = new InputBox();
            input.Title = title;
            input.Owner = _Parent;
            if (input.ShowDialog(description, value, maxLength).Value)
              Result=input.Value;
            else
              Result=null;
          });
    
          return Result;
        }
    
        //Call this somewhere at application startup so that the dialog boxes
        //appear as child windows.
        public void SetParentWindow(Window parent)
        {
          _Parent = parent;
        }
      }
    }
    
  • 4

    有两种很好的方法可以做到这一点,1)对话服务(简单,干净),2)视图辅助 . View辅助提供了一些简洁的功能,但通常不值得 .

    对话服务

    a)一个对话框服务接口,如via构造函数或一些依赖容器:

    interface IDialogService { Task ShowDialogAsync(DialogViewModel dlgVm); }

    b)您的IDialogService实现应打开一个窗口(或将一些控件注入活动窗口),创建一个对应于给定dlgVm类型名称的视图(使用容器注册或约定或具有类型的ContentPresenter)相关的DataTemplates) . ShowDialogAsync应该创建一个TaskCompletionSource并返回它的.Task proptery . 当您想要关闭时,DialogViewModel类本身需要一个可以在派生类中调用的事件,并在对话框视图中观察以实际关闭/隐藏对话框并完成TaskCompletionSource .

    b)要使用,只需在您的某个DialogViewModel派生类的实例上调用await this.DialogService.ShowDialog(myDlgVm) . 等待返回后,查看您在对话框VM上添加的属性,以确定发生了什么;你甚至不需要回调 .

    查看已获得帮助

    这使您可以在viewmodel上侦听事件 . 这可以全部包含在混合行为中,以避免代码落后和资源使用,如果你是如此倾向(FMI,子类“行为”类,以查看类固醇上的一种Blendable附加属性) . 现在,我们将在每个视图上手动执行此操作:

    a)使用自定义有效负载(DialogViewModel派生类)创建OpenXXXXXDialogEvent .

    b)让视图在其OnDataContextChanged事件中订阅该事件 . 如果旧值!= null并且在Window的Unloaded事件中,请务必隐藏和取消订阅 .

    c)当事件触发时,让视图打开您的视图,该视图可能位于页面上的资源中,或者您可以按照惯例在其他位置找到它(例如在对话框服务方法中) .

    这种方法更灵活,但需要更多的工作才能使用 . 我不太习惯 . 例如,一个很好的优点是能够将视图物理地放置在选项卡中 . 我已经使用算法将其放在当前用户控件的边界中,或者如果不够大,则遍历可视树,直到找到足够大的容器 .

    这允许对话框靠近他们实际使用的位置,只调暗与当前活动相关的应用程序部分,让用户在应用程序内移动而不必手动推送对话框,甚至有多个准对象模式对话框在不同的选项卡或子视图上打开 .

  • 0

    使用freezable命令

    <Grid>
            <Grid.DataContext>
                <WpfApplication1:ViewModel />
            </Grid.DataContext>
    
    
            <Button Content="Text">
                <Button.Command>
                    <WpfApplication1:MessageBoxCommand YesCommand="{Binding MyViewModelCommand}" />
                </Button.Command>
            </Button>
    
    </Grid>
    
    public class MessageBoxCommand : Freezable, ICommand
    {
        public static readonly DependencyProperty YesCommandProperty = DependencyProperty.Register(
            "YesCommand",
            typeof (ICommand),
            typeof (MessageBoxCommand),
            new FrameworkPropertyMetadata(null)
            );
    
    
        public static readonly DependencyProperty OKCommandProperty = DependencyProperty.Register(
            "OKCommand",
            typeof (ICommand),
            typeof (MessageBoxCommand),
            new FrameworkPropertyMetadata(null)
            );
    
    
        public static readonly DependencyProperty CancelCommandProperty = DependencyProperty.Register(
            "CancelCommand",
            typeof (ICommand),
            typeof (MessageBoxCommand),
            new FrameworkPropertyMetadata(null)
            );
    
    
        public static readonly DependencyProperty NoCommandProperty = DependencyProperty.Register(
            "NoCommand",
            typeof (ICommand),
            typeof (MessageBoxCommand),
            new FrameworkPropertyMetadata(null)
            );
    
    
        public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
            "Message",
            typeof (string),
            typeof (MessageBoxCommand),
            new FrameworkPropertyMetadata("")
            );
    
        public static readonly DependencyProperty MessageBoxButtonsProperty = DependencyProperty.Register(
            "MessageBoxButtons",
            typeof(MessageBoxButton),
            typeof(MessageBoxCommand),
            new FrameworkPropertyMetadata(MessageBoxButton.OKCancel)
            );
    
        public ICommand YesCommand
        {
            get { return (ICommand) GetValue(YesCommandProperty); }
            set { SetValue(YesCommandProperty, value); }
        }
    
        public ICommand OKCommand
        {
            get { return (ICommand) GetValue(OKCommandProperty); }
            set { SetValue(OKCommandProperty, value); }
        }
    
        public ICommand CancelCommand
        {
            get { return (ICommand) GetValue(CancelCommandProperty); }
            set { SetValue(CancelCommandProperty, value); }
        }
    
        public ICommand NoCommand
        {
            get { return (ICommand) GetValue(NoCommandProperty); }
            set { SetValue(NoCommandProperty, value); }
        }
    
        public MessageBoxButton MessageBoxButtons
        {
            get { return (MessageBoxButton)GetValue(MessageBoxButtonsProperty); }
            set { SetValue(MessageBoxButtonsProperty, value); }
        }
    
        public string Message
        {
            get { return (string) GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }
    
        public void Execute(object parameter)
        {
            var messageBoxResult = MessageBox.Show(Message);
            switch (messageBoxResult)
            {
                case MessageBoxResult.OK:
                    OKCommand.Execute(null);
                    break;
                case MessageBoxResult.Yes:
                    YesCommand.Execute(null);
                    break;
                case MessageBoxResult.No:
                    NoCommand.Execute(null);
                    break;
                case MessageBoxResult.Cancel:
                    if (CancelCommand != null) CancelCommand.Execute(null); //Cancel usually means do nothing ,so can be null
                    break;
    
            }
        }
    
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
        public event EventHandler CanExecuteChanged;
    
    
        protected override Freezable CreateInstanceCore()
        {
            throw new NotImplementedException();
        }
    }
    
  • 1

    我认为对话框的处理应该是视图的责任,视图需要有代码来支持它 .

    如果更改ViewModel - View交互以处理对话框,则ViewModel依赖于该实现 . 解决此问题的最简单方法是使View负责执行任务 . 如果这意味着显示一个对话框然后很好,但也可能是状态栏中的状态消息等 .

    我的观点是,MVVM模式的重点在于将业务逻辑与GUI分离,因此您不应该在业务层(ViewModel)中混合使用GUI逻辑(以显示对话框) .

  • 0

    一个有趣的替代方法是使用负责显示视图(对话框)的控制器 .

    如何工作由 WPF Application Framework (WAF) 显示 .

  • 16

    为什么不在VM中引发事件并在视图中订阅事件?这将使应用程序逻辑和视图保持分离,并且仍允许您使用子窗口进行对话 .

  • 1

    我已经实现了一个侦听来自ViewModel的Message的Behavior . 它基于Laurent Bugnion解决方案,但由于它不使用代码并且更可重用,我认为它更优雅 .

    How to make WPF behave as if MVVM is supported out of the box

  • 6

    我认为视图可以有代码来处理视图模型中的事件 .

    根据事件/场景,它还可以具有订阅视图模型事件的事件触发器,以及响应中要调用的一个或多个动作 .

  • 2

    我有相同的情况,并将MessageBox包装到设计器的隐形控件中 . 细节在我的博客中

    http://geekswithblogs.net/mukapu/archive/2010/03/12/user-prompts-messagebox-with-mvvm.aspx

    同样可以扩展到任何模态对话框,文件浏览控件等 .

  • 5

    我在这个问题的答案中描述了我自己的窗口加载器:

    Managing multiple WPF views in an application

  • 3

    Karl Shifflett创建了一个示例应用程序,用于使用服务方法和Prism InteractionRequest方法显示对话框 .

    我喜欢这种服务方法 - 它不太灵活,因此用户不太可能破坏某些东西:)它也与我的应用程序的WinForms部分一致(MessageBox.Show)但是如果你打算展示很多不同的对话框,那么InteractionRequest就是一个更好的方式去 .

    http://karlshifflett.wordpress.com/2010/11/07/in-the-box-ndash-mvvm-training/

  • 2

    我知道这是一个老问题,但是当我进行这个搜索时,我发现了很多相关问题,但我没有找到一个非常明确的回答 . 所以我自己实现了dialogbox / messagebox / popin,我分享了!
    我认为它是"MVVM proof",我试着让它变得简单和正确,但我是WPF的新手,所以请随意发表评论,甚至提出拉取请求 .

    https://github.com/Plasma-Paris/Plasma.WpfUtils

    你可以像这样使用它:

    public RelayCommand YesNoMessageBoxCommand { get; private set; }
    async void YesNoMessageBox()
    {
        var result = await _Service.ShowMessage("This is the content of the message box", "This is the title", System.Windows.MessageBoxButton.YesNo);
        if (result == System.Windows.MessageBoxResult.Yes)
            // [...]
    }
    

    或者像这样,如果你想要更复杂的popin:

    var result = await _Service.ShowCustomMessageBox(new MyMessageBoxViewModel { /* What you want */ });
    

    它显示的是这样的事情:

    2

  • 3

    在询问how the view model for a task or dialog should look like时,我正在思考类似的问题 .

    我目前的解决方案如下:

    public class SelectionTaskModel<TChoosable> : ViewModel
        where TChoosable : ViewModel
    {
        public SelectionTaskModel(ICollection<TChoosable> choices);
        public ReadOnlyCollection<TChoosable> Choices { get; }
        public void Choose(TChoosable choosen);
        public void Abort();
    }
    

    当视图模型决定需要用户输入时,它会提取 SelectionTaskModel 的实例以及用户可能的选择 . 基础设施负责调出相应的视图,在适当的时候将根据用户的选择调用 Choose() 函数 .

  • 3

    我在同样的问题上挣扎 . 我想出了一种在View和ViewModel之间进行相互通信的方法 . 您可以启动从ViewModel向View发送消息,告诉它显示消息框,然后它将报告结果 . 然后ViewModel可以响应从View返回的结果 .

    我在my blog中证明了这一点:

  • 1

    我写了一篇关于这个主题的相当全面的文章,并为MVVM Dialogs开发了一个pop-in库 . 严格遵守MVVM不仅可行,而且在正确实施时非常干净,并且可以轻松扩展到不依赖于它的第三方库:

    https://www.codeproject.com/Articles/820324/Implementing-Dialog-Boxes-in-MVVM

  • 131

    对不起,但我必须要插入 . 在Prism项目中找到Prism.Wpf.Interactivity命名空间之前,我已经完成了几个建议的解决方案 . 您可以使用交互请求和弹出窗口操作来滚动自定义窗口,或者在“通知和确认”弹出窗口中内置更简单的需求 . 这些创建真正的窗口并按此管理 . 您可以在对话框中传递具有所需依赖项的上下文对象 . 自从我找到它以来,我们在我的工作中使用了这个解我们这里有很多资深开发人员,没有人能想出更好的东西 . 我们之前的解决方案是将对话框服务放入叠加层并使用演示者类来实现它,但是您必须为所有对话框视图模型等设置工厂 .

    这不是微不足道的,但也不是非常复杂 . 它是内置于Prism,因此最好(或更好)实践恕我直言 .

    我2美分!

  • 0

    编辑:是的我同意这不是一个正确的MVVM方法,我现在使用类似于blindmeis建议的东西 .

    你能做到这一点的方法之一是

    在主视图模型中(打开模态的位置):

    void OpenModal()
    {
        ModalWindowViewModel mwvm = new ModalWindowViewModel();
        Window mw = new Window();
        mw.content = mwvm;
        mw.ShowDialog()
        if(mw.DialogResult == true)
        { 
            // Your Code, you can access property in mwvm if you need.
        }
    }
    

    在你的模态窗口视图/ ViewModel中:

    XAML:

    <Button Name="okButton" Command="{Binding OkCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">OK</Button>
    <Button Margin="2" VerticalAlignment="Center" Name="cancelButton" IsCancel="True">Cancel</Button>
    

    视图模型:

    public ICommand OkCommand
    {
        get
        {
            if (_okCommand == null)
            {
                _okCommand = new ActionCommand<Window>(DoOk, CanDoOk);
            }
            return _okCommand ;
        }
    }
    
    void DoOk(Window win)
    {
        <!--Your Code-->
        win.DialogResult = true;
        win.Close();
    }
    
    bool CanDoOk(Window win) { return true; }
    

    或类似于此处发布的内容WPF MVVM: How to close a window

相关问题