WPF绑定到自身 [英] WPF Bind to itself

查看:199
本文介绍了WPF绑定到自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆