如何使用 MVVM Light Toolkit 通过取消按钮关闭 ChildWindow
我是 MVVM 新手,试图弄清楚如何使用 MVVM Light Toolkit 通过传统的取消按钮来关闭 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");
}
I'm new to MVVM and trying to figure out how to close a ChildWindow with the traditional Cancel button using MVVM Light Toolkit.
In my ChildWindow (StoreDetail.xaml), I have :
<Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelCommand}" />
In my ViewModel (ViewModelStoreDetail.cs), I have :
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您通过调用 ShowDialog() 显示子窗口,则只需将按钮控件的 IsCancel 属性设置为“True”即可。
它与单击窗口上的 X 按钮或按键盘上的 ESC 相同。
If you displayed your child window by calling ShowDialog(), then you can simply set the IsCancel property of your button control to "True".
It becomes the same as clicking the X button on the window, or pressing ESC on the keyboard.
请查看MSDN 上的这篇文章。大约一半的地方有一个关于如何做到这一点的方法。基本上它使用
WorkspaceViewModel
或您实现一个公开和事件RequestClose
的接口,然后您可以在 Window 的 DataContext 内部(如果您将 ViewModel 设置为它)附加到活动。
这是文章的摘录(图 7)。您可以调整它以满足您的需要。
Have a look at this articleon MSDN. About half way down there is an approach on how to do this. Basically it uses either uses a
WorkspaceViewModel
or you implements an interface that exposes and eventRequestClose
You then inside the Window's DataContext (if you are setting the ViewModel to it) you can attach to the event.
This is an excerpt from the article (Figure 7). You can adjust it to suit your needs.
我已经有一段时间没有使用 WPF 和 MVVMLight 了,但是我想我应该使用消息发送器来发送取消事件。
It's been a while since I've used WPF and MVVMLight but yes I think I'd use the messanger to send the cancel event.
在MVVM Light Toolkit中,您能做的最好的事情就是使用
Messenger
与View
进行交互。只需在
View
中注册 close 方法(通常在代码隐藏文件中),然后在需要时发送关闭窗口的请求。In MVVM Light Toolkit the best what you can do is to use
Messenger
to interact with theView
.Simply register close method in the
View
(typically in the code behind file) and then send request to close a window when you need it.我们实施了无代码功能背后。看看是否有帮助。
编辑:这里有 Stackoverflow 讨论
We have implemented a NO-CODE BEHIND functionality. See if it helps.
EDIT: Here is there Stackoverflow discussion
这里有一些方法可以实现它。
Here are some ways to accomplish it.
参加聚会有点晚了,但我想我应该补充一下我的意见。借用 user841960 的答案:
然后:
然后:
它比使用 ICommand 更干净,而且效果也一样。
因此,总而言之,示例类如下所示:
Kind of late to the party but I thought I'd add my input. Borrowing from user841960's answer:
Then:
Then:
It's a bit cleaner than using an ICommand and works just as well.
So, to sum it all up, the example class would look like so: