7

我是 MVVM 的新手,并试图弄清楚如何使用MVVM Light Toolkit使用传统的Cancel按钮关闭 ChildWindow

在我的 ChildWindow (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");
}
4

8 回答 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();
}
于 2011-07-13T04:12:43.373 回答
1

如果您通过调用 ShowDialog() 来显示您的子窗口,那么您可以简单地将按钮控件的 IsCancel 属性设置为“True”。

<Button Content="Cancel" IsCancel="True" />

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

于 2012-09-19T22:22:45.460 回答
0

MVVM Light Toolkit中,您能做的最好的事情就是Messenger使用View.

只需在View(通常在文件后面的代码中)注册 close 方法,然后在需要时发送关闭窗口的请求。

于 2011-04-12T20:51:33.127 回答
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();
    }
}
于 2013-08-14T18:15:22.160 回答
0

我们已经实现了NO-CODE BEHIND 功能。看看有没有帮助。

编辑:这里有Stackoverflow讨论

于 2011-08-12T23:58:39.303 回答
0

这里有一些方法来完成它。

  1. 向您的子窗口发送消息,并在子窗口代码隐藏上将 DialogueResult 设置为 false。
  2. 制作DialogueResult的属性并将其与childwindow Dialoue CLR属性绑定,设置在CancelCommand的CancelEval方法上。
  3. 创建 Childwindow 对象并在 CancelEval 上将 DialogueResult 设置为 false。
于 2012-09-08T16:38:59.547 回答
0

自从我使用 WPF 和 MVVMLight 以来已经有一段时间了,但是是的,我想我会使用 messanger 来发送取消事件。

于 2011-04-12T20:29:06.867 回答
0

在MSDN上查看这篇文章。大约一半的时候,有一种方法可以做到这一点。基本上它要么使用 aWorkspaceViewModel要么你实现一个暴露和事件的接口RequestClose

然后,您可以在 Window 的 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();
}
于 2011-04-12T18:09:43.743 回答