WPF 自身绑定

我有一个 WPF Window,在某个地方有一个 ListView,在那里我绑定了一个 List<string>

现在,在我的 ListView中有一个 TextBox,并且 Content属性被设置为 {Binding}

但是这只是一个简短的说明,我如何编写完整的绑定来绑定到它自己呢?

{Binding Path=Self}不起作用,{Binding Self}也不起作用(后者是前者的快捷方式)。

104196 次浏览

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}" />

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=.).

another way to bind to self :

{Binding {}}

an empty {} binds to the RelativeSource.Self.