了解 WPF 绑定 CommandParameter="{Binding}"; [英] Understand WPF Binding CommandParameter="{Binding}"
问题描述
也许这是一个愚蠢的问题,但我找不到答案:在下面的 xaml 中,CommandParameter
绑定到什么?或者一般来说,"{Binding}"
是什么意思?
Maybe this is a dumb question, but I can't find the answer: in the following xaml what does CommandParameter
binding to? Or in general, what does "{Binding}"
mean?
<Button Command="{Binding DataContext.DeleteCommand, ElementName=List}"
CommandParameter="{Binding}"/>
推荐答案
{Binding ...}
是一个 MarkupExtension.
在其通常的形式中,它采用类似 {Binding Path=someProperty, ...}
的 Path(或其缩写形式 {Binding someProperty, ...}
).
所以 {Binding}
中的路径是空的,这意味着 Binding 绑定到任何用于 Binding 的 Source.如果您知道 {Binding}
实际上与 {Binding DataContext,RelativeSource={RelativeSource Self}}
相同,这可能更容易理解.
{Binding ...}
is a MarkupExtension.
In its usual form it takes a Path like {Binding Path=someProperty, ...}
(or its short form {Binding someProperty, ...}
).
So the path in {Binding}
is empty which means the Binding is bound to whatever Source there is for the Binding. This might be a little easier to understand if you know, that {Binding}
is actually the same as {Binding DataContext,RelativeSource={RelativeSource Self}}
.
因此,在您的情况下,CommandParameter 获取按钮的当前 DataContext 的值.
So in your case CommandParameter gets the value of the current DataContext of the Button.
这篇关于了解 WPF 绑定 CommandParameter="{Binding}";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!