如何关闭命令绑定到一个按钮 [英] How to bind Close command to a button

查看:131
本文介绍了如何关闭命令绑定到一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最简单的方式是贯彻落实 ButtonClick 事件处理程序,并调用 window.close()的方法。但如何通过做到这一点命令绑定?

The easiest way is to implement ButtonClick event handler and invoke Window.Close() method. But how to do 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:

href=\"http://msdn.microsoft.com/en-us/magazine/dd419663.aspx\">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}}}"/>

这篇关于如何关闭命令绑定到一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆