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

查看:33
本文介绍了将当前窗口作为 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?-" />

推荐答案

我可以想到两种方法来做到这一点: 为窗口命名(通过窗口上的 x:Name 属性Window 标签,然后构造一个这样的绑定(假设窗口的名字是'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}" />

对于更一般的东西(不依赖于为当前窗口命名),可以像这样构造绑定:

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天全站免登陆