如何使用MVVM Light Toolkit关闭一个带有取消按钮的ChildWindow

7

我刚接触MVVM并试图弄清如何使用传统的取消按钮和MVVM Light Toolkit关闭子窗口

在我的子窗口(StoreDetail.xaml)中,我有:

<Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelCommand}" />

在我的ViewModel(ViewModelStoreDetail.cs)中,我有以下内容:
public ICommand CancelCommand { get; private set; }

public ViewModelStoreDetail()
{
    CancelCommand = new RelayCommand(CancelEval);
}

private void CancelEval()
{
    //Not sure if Messenger is the way to go here...
    //Messenger.Default.Send<string>("ClosePostEventChildWindow", "ClosePostEventChildWindow");
}
8个回答

1
private DelegateCommand _cancelCommand;

public ICommand CancelCommand
{
    get
    {
        if (_cancelCommand == null)
            _cancelCommand = new DelegateCommand(CloseWindow);
        return _cancelCommand;
    }
}

private void CloseWindow()
{
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}

1
如果您通过调用ShowDialog()方法显示了子窗口,则可以将按钮控件的IsCancel属性设置为“True”。
<Button Content="Cancel" IsCancel="True" />

这将与单击窗口上的X按钮或按键盘上的ESC键相同。


0

请查看这篇MSDN文章。在大约一半的位置上,有一个关于如何实现它的方法。基本上它使用了一个WorkspaceViewModel或者您可以实现一个公开事件RequestClose的接口

然后在窗口的DataContext中(如果您将ViewModel设置为它)您可以附加到事件。

这是来自文章的摘录(图7)。 您可以根据需要进行调整。

// In App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

MainWindow window = new MainWindow();

// Create the ViewModel to which 
// the main window binds.
string path = "Data/customers.xml";
var viewModel = new MainWindowViewModel(path);

// When the ViewModel asks to be closed, 
// close the window.
viewModel.RequestClose += delegate 
{ 
    window.Close(); 
};

// Allow all controls in the window to 
// bind to the ViewModel by setting the 
// DataContext, which propagates down 
// the element tree.
window.DataContext = viewModel;

window.Show();
}

0
有点晚了,但我想加入我的意见。借鉴自user841960的答案:
public RelayCommand CancelCommand
{
    get;
    private set;
}

然后:

SaveSettings = new RelayCommand(() => CloseWindow());

然后:

private void CloseWindow()
{
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}

这比使用ICommand更加简洁,而且同样有效。

因此,总结一下,示例类将如下所示:

public class ChildViewModel
{
    public RelayCommand CancelCommand
    {
        get;
        private set;
    }

    public ChildViewModel()
    {
        SaveSettings = new RelayCommand(() => CloseWindow());
    }

    private void CloseWindow()
    {
        Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
    }
}

SaveSettings = ...应该放在哪里? - admiral142
在ViewModel构造函数中。可以在此处找到完整的示例,以及Command、RelayCommand和EventToCommand文档。 - DanteTheEgregore

0

我有一段时间没有使用WPF和MVVMLight了,但我认为我会使用messanger来发送取消事件。


0
在MVVM Light Toolkit中,最好的做法是使用Messenger与View进行交互。
只需在View中注册关闭方法(通常在代码后置文件中),然后在需要时发送关闭窗口的请求。

0

以下是一些实现方法。

  1. 向您的子窗口发送消息,并在子窗口代码后端将DialogueResult设置为false。
  2. 创建DialogueResult属性并将其与子窗口Dialoue CLR属性绑定,在CancelCommand的CancelEval方法中设置它。
  3. 创建Childwindow对象并在CancelEval中将DialogueResult设置为false。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接