本文介绍了如何将关闭命令绑定到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最简单的方法是实现 ButtonClick
事件处理程序并调用 Window.Close()
方法,但是如何通过 Command
绑定来做到这一点?
The easiest way is to implement ButtonClick
event handler and invoke Window.Close()
method, but how doing this through a Command
binding?
推荐答案
我认为在现实世界的场景中,一个简单的点击处理程序可能比过于复杂的基于命令的系统更好,但你可以这样做:
I think that in real world scenarios a simple click handler is probably better than over-complicated command-based systems but you can do something like that:
使用本文中的 RelayCommand http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
using RelayCommand from this article http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
public class MyCommands
{
public static readonly ICommand CloseCommand =
new RelayCommand( o => ((Window)o).Close() );
}
<Button Content="Close Window"
Command="{X:Static local:MyCommands.CloseCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}}"/>
这篇关于如何将关闭命令绑定到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!