更新时间:2021-11-12 03:02:10
It's:
{Binding DataContext.FireCommand,
RelativeSource={RelativeSource AncestorType=ListBox}}
除非您沿途实际更改了 DataContext
,否则无需走到根目录,但由于 ListBox
似乎绑定到主 VM 上的属性,因此应该够了.
No need to walk up to the root unless you actually change the DataContext
along the way, but as the ListBox
seems to bind to a property on the main VM this should be enough.
我唯一推荐阅读的是数据绑定概述 和 Binding
类 文档(包括其属性).
The only thing i recommend reading is the Data Binding Overview, and the Binding
class documentation (including its properties).
这里还有一个关于如何构建绑定的简短解释:一个绑定由一个 source 和一个相对于那个 Path
组成>source,默认情况下 source 是当前的 DataContext
.可以显式设置的源有:Source
、ElementName
&相对源
.设置其中任何一个都会将 DataContext
覆盖为 source.
Also here is a short explanation on how bindings are constructed: A binding consists of a source and a Path
relative to that source, by default the source is the current DataContext
. Sources that can be set explicitly are: Source
, ElementName
& RelativeSource
. Setting any of those will override the DataContext
as source.
因此,如果您使用 source 之类的 RelativeSource
并希望在该级别访问 DataContext
中的某些内容,DataContext
需要出现在Path
中.
So if you use a source like RelativeSource
and want to access something in the DataContext
on that level the DataContext
needs to appear in the Path
.