Swi*_*der 24 wpf binding commandparameter
如何将当前窗口作为参数传递给命令?
我喜欢在XAML-markup中执行此操作:
<Button Command="CommandGetsCalled" CommandParameter="-this?-" />
Run Code Online (Sandbox Code Playgroud)
Dan*_*att 66
我可以通过两种方式来做到这一点:给窗口命名(通过标签上的x:Name属性Window,然后构造这样的绑定(假设窗口的名称是'ThisWindow'):
<Button Command="CommandGetsCalled" CommandParameter="{Binding ElementName=ThisWindow}" />
Run Code Online (Sandbox Code Playgroud)
对于更通用的东西(不依赖于给当前Window命名),绑定可以像这样构造:
<Button Command="CommandGetsCalled" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
Run Code Online (Sandbox Code Playgroud)
Rac*_*hel 21
您可以尝试绑定到RelativeSource
如果要将Button作为参数传递:
<Button Command="CommandGetsCalled"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
Run Code Online (Sandbox Code Playgroud)
如果要将Window作为参数传递:
<Button Command="CommandGetsCalled"
CommandParameter="{Binding RelativeSource={
RelativeSource AncestorType={x:Type Window}}}" />
Run Code Online (Sandbox Code Playgroud)
在我的情况下,提供的答案都没有奏效。
这对我有用:
<window x:Name="myWindow">
<Button Command="Command" CommandParameter={x:Reference Name=myWindow}/>
</window>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21766 次 |
| 最近记录: |