将当前窗口作为CommandParameter传递 [英] passing the current Window as a CommandParameter

查看:403
本文介绍了将当前窗口作为CommandParameter传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将当前打开的窗口作为参数传递给命令?

how can I pass the window I am currently on as a parameter to a command?

我喜欢在XAML标记中这样做:

I like to do this in XAML-markup:

<Button Command="CommandGetsCalled" CommandParameter="-this?-" />

推荐答案

我可以想到两种方法:为窗口命名(通过Window标记上的x:Name属性,然后构造一个这样的绑定(假设窗口的名称是"ThisWindow"):

There are two ways I can of think to do this: Give the window a name (via a x:Name attribute on the Window tag, and then construct a binding like this (assumes the name of the window is 'ThisWindow'):

<Button Command="CommandGetsCalled" CommandParameter="{Binding ElementName=ThisWindow}" />

对于更通用的东西(不依赖于给当前Window命名),可以这样构造绑定:

For something more general (doesn't rely on giving the current Window a name), the binding can be constructed like this:

<Button Command="CommandGetsCalled" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" /> 

这篇关于将当前窗口作为CommandParameter传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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