了解 WPF 绑定 CommandParameter="{Binding}"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5377690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:41:07  来源:igfitidea点击:

Understand WPF Binding CommandParameter="{Binding}"

wpfbinding

提问by Bolu

Maybe this is a dumb question, but I can't find the answer: in the following xaml what does CommandParameterbinding to? Or in general, what does "{Binding}"mean?

也许这是一个愚蠢的问题,但我找不到答案:在下面的 xaml 中CommandParameter绑定到什么?或者一般来说,是什么"{Binding}"意思?

<Button Command="{Binding DataContext.DeleteCommand, ElementName=List}" 
        CommandParameter="{Binding}"/>

回答by Markus Hütter

{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}}.

{Binding ...}是一个标记扩展。
在其通常的形式中,它采用 Path 之类的{Binding Path=someProperty, ...}(或其缩写形式{Binding someProperty, ...})。
所以路径{Binding}是空的,这意味着绑定绑定到绑定的任何源。如果您知道,这可能更容易理解,这{Binding}实际上与{Binding DataContext,RelativeSource={RelativeSource Self}}.

So in your case CommandParameter gets the value of the current DataContext of the Button.

因此,在您的情况下,CommandParameter 获取按钮的当前 DataContext 的值。

回答by biju

An Empty {Binding}will pass the current DataContext of the control to the Executed and CanExecute methods respectively.

Empty{Binding}将分别将控件的当前 DataContext 传递给 Executed 和 CanExecute 方法。