将当前窗口作为命令参数传递
如何将当前所在的窗口作为参数传递给命令?
我喜欢在 XAML 标记中执行此操作:
<Button Command="CommandGetsCalled" CommandParameter="-this?-" />
how can I pass the window I am currently on as a parameter to a command?
I like to do this in XAML-markup:
<Button Command="CommandGetsCalled" CommandParameter="-this?-" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以想到两种方法来做到这一点: 为窗口命名(通过
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 theWindow
tag, and then construct a binding like this (assumes the name of the window is 'ThisWindow'):For something more general (doesn't rely on giving the current Window a name), the binding can be constructed like this:
您可以尝试绑定到RelativeSource
如果您想将Button 作为参数传递:
如果您想将Window 作为参数传递:
You could try binding to a RelativeSource
If you want to pass the Button as a parameter:
If you want to pass the Window as a parameter:
在我的情况下,所提供的答案都不起作用。
这对我有用:
In my situation none of the provided answers worked.
This worked for me: