WPF 绑定到自身 [英] WPF Bind to itself
问题描述
我有一个 WPF Window
,在某个地方有一个 ListView
,我在其中绑定了一个 List
.>
现在在我的 ListView
中有一个 TextBox
并且 Content
属性设置为 {Binding}
.
但这是简写.如何编写完整绑定以绑定到自身?
{Binding Path=Self}
不起作用,{Binding Self}
也不起作用(后者是前者的捷径).
简短回答:{Binding}
不是的快捷方式"绑定到自身"(在 RelativeSource 的意义上.自己).相反,{Binding}
相当于 {Binding Path=.}
,绑定到当前源.
详细说明:一个绑定有一个源和一个路径.您可以执行绑定到自身",例如,使用
<myUIControl myProperty="{Binding RelativeSource={RelativeSource Self}, Path=x}"/>
然而,这会将 source 设置为控件本身,因此它将尝试访问 UI 控件的属性 x
(而不是属性 x
当前数据上下文).从我对你的问题的理解来看,这不是你想要的;特别是,它不是 {Binding}
所做的: {Binding}
保持源的原样(通常是某个父元素的 DataContext
) 并绑定到源本身(相当于 Path=.
).
I've got a WPF Window
, and somewhere there is a ListView
where I bind a List<string>
to.
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}
doesn't work, neither does {Binding Self}
(where the latter is a shortcut for the former).
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=.
).
这篇关于WPF 绑定到自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!