WPF绑定到自身 [英] WPF Bind to itself
问题描述
我有一个WPF 窗口
,某处有一个 ListView
我绑定一个列表< string>
to。
I've got a WPF Window
, and somewhere there is a ListView
where I bind a List<string>
to.
现在我的 ListView
是一个 TextBox
,而 Content
属性设置为 {Binding}
。
Now somewhere in my ListView
there is a TextBox
and the Content
property is set to {Binding}
.
但这是速记。如何编写绑定到本身的完整绑定?
But this is the shorthand. How do I write the full binding to bind to itself?
{Binding Path = Self}
不起作用, {Binding Self }
(其中后者是前者的快捷方式)
{Binding Path=Self}
doesn't work, neither does {Binding Self}
(where the latter is a shortcut for the former).
推荐答案
: {Binding}
不是绑定到自己的快捷方式(在 RelativeSource.Self )。相反, {Binding}
等价于 {Binding Path =。}
,其绑定到当前的源。
Short answer:{Binding}
is not a shortcut for "binding to itself" (in the sense of RelativeSource.Self). Rather, {Binding}
is equivalent to {Binding Path=.}
, which binds to the current source.
详细说明:绑定具有源和路径。您可以通过使用
To elaborate: A binding has a source and a path. You can do a "binding to itself", for example, by using
<myUIControl myProperty="{Binding RelativeSource={RelativeSource Self}, Path=x}" />
然而,将源设置为控件本身,因此将尝试访问UI控件的属性 x
(而不是当前数据上下文的属性 x
)。从我怎么理解你的问题,这不是你想要的;特别是,它不是什么 {Binding}
: {Binding}
保持原始码(通常是 c $ c> c $ c>),并绑定到源本身(相当于 Path =。
)
This, however, sets the source to the control itself, so it will try to access property x
of the UI control (rather than property x
of the current data context). From how I understood your question, this is not what you want; in particular, it is not what {Binding}
does: {Binding}
keeps the source as it is (usually the DataContext
of some parent element) and binds to the source itself (equivalent to Path=.
).
这篇关于WPF绑定到自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!