WPF 绑定到自身

发布于 2024-08-14 13:53:33 字数 354 浏览 3 评论 0原文

我有一个 WPF Window,并且某处有一个 ListView,我在其中绑定了 List

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

但这是简写。如何编写完整的绑定来绑定到自身?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆依然 2024-08-21 13:53:33

简短回答{Binding}不是“绑定到自身”的快捷方式(在RelativeSource.Self)。相反,{Binding} 相当于 {Binding Path=.},绑定到当前源。


详细说明:绑定具有路径。您可以执行“绑定到自身”,例如,通过使用

<myUIControl myProperty="{Binding RelativeSource={RelativeSource Self}, Path=x}" />

This,但是,将设置为控件本身,因此它将尝试访问属性x UI 控件(而不是当前数据上下文的属性 x)。从我对你的问题的理解来看,这不是你想要的;特别是,它不是 {Binding} 所做的:{Binding} 保持源原样(通常是某些父元素的 DataContext )并绑定到源本身(相当于 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}" />

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

浮云落日 2024-08-21 13:53:33

绑定到 self 的另一种方法:

{Binding {}}

空的 {} 绑定到relativeSource.Self。

another way to bind to self :

{Binding {}}

an empty {} binds to the RelativeSource.Self.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文