为什么将窗口的 DataContext 设置为 {BindingrelativeSource={RelativeSource Self}} 允许您将数据绑定到代码隐藏的属性?
根据我的理解,将DataContext
设置为控件本身将允许您使用数据绑定访问该控件的属性,而无需指定source
窗口的DataContext
to {binding reactricesource = {reactraction self}}
只能允许您数据绑定到window
window中定义的属性< /code>类,而不是代码主题中的类别,因为代码范围仅继承了
window
class,而我在代码范围文件中定义的属性不直接属于窗口
类。但是,当我这样做时,它神奇地允许我数据绑定到我在代码范围文件中定义的属性。这是如何运作的?
According to my understanding, setting the DataContext
to a control itself will allow you to access the property of that control using data binding without specifying the Source
, so setting the DataContext
of a Window
to {Binding RelativeSource={RelativeSource Self}}
should only allow you to data bind to the properties defined in the Window
class, not the ones in the code-behind because the code-behind file only inherited the Window
class, and the properties I defined in the code-behind file don't directly belong to the Window
class. However, when I do it, it magically allows me to data bind to the properties I defined in the code-behind file. How does this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,将控件的
DataContext
设置为自身允许您在Binding
中访问其属性。隐藏代码
在 XAML 中创建
绑定
将自动继承此DataContext
作为绑定的Source
(如果不是)覆盖,例如将项目设置为ItemsControl
中的DataContext
。<块引用>
数据上下文是一个概念,它允许元素从其父元素继承有关用于绑定的数据源的信息,以及绑定的其他特征(例如路径)。 [...]
XAML
RelativeSource
会将Binding
的Source
设置为窗口本身。如果您使用AncestorType={x:Type Window}
,情况也是如此。<块引用>
通过指定绑定源相对于绑定目标位置的位置来获取或设置绑定源。
您无法在单个绑定上设置多个不同的源。
<块引用>
RelativeSource
:与ElementName
和Source
互斥;本质上,如果不设置
Source
,则永远不会绑定,它是隐式或显式设置的。如果您指定
Window
作为源,为什么Binding
可以工作?您必须了解,XAML 文件和代码隐藏在编译后是同一个类。它们都是在编译时合并的部分
定义。您继承自Window
是对的,但您对绑定的理解是错误的。如果我们谈论类继承,那么您是对的,通过Window
引用访问派生的MyWindow
不允许您访问其属性,但绑定不能以这种方式工作。它们是表达式,将依赖属性松散地耦合到被绑定的属性。它们仅解决在运行时,这意味着它们应用于DataContext
的实际运行时类型,即我的窗口
。您可以将任意
对象
分配和切换为DataContext
,甚至可以是不同类型的。只要任何绑定的Path
与任何属性值匹配,就会成功解析,否则会出现绑定错误。Yes, setting the
DataContext
of a control to itself allows you to access its properties in aBinding
.Code-Behind
Creating a
Binding
in XAML will automatically inherit thisDataContext
asSource
for binding if it is not overridden e.g. an item is set asDataContext
in anItemsControl
.XAML
The
RelativeSource
will set theSource
of theBinding
to the window itself. The same would be true, if you would useAncestorType={x:Type Window}
.You cannot set multiple different sources on a single binding.
In essence, the you never bind without setting a
Source
, it is set implicitly or explicitly.Why does the
Binding
work then, if you specify aWindow
as source? You have to understand, that the XAML file and your code-behind are one and the same class after compilation. They are bothpartial
definitions that are merged on compilation. You are right that you inherit fromWindow
, but your understanding of bindings is wrong. If we talk about class inheritance, you are right, accessing a derivedMyWindow
through aWindow
reference would not allow you to access its properties, but bindings do not work this way. They are expressions that loosley couple dependency properties to properties being bound. They are only resolved at run-time, which means they are applied to the actual run-time type of theDataContext
, which isMyWindow
.You could assign and switch any arbitrary
object
asDataContext
even of different types. As long as thePath
of any binding matches any property value it will be resolved successfully, otherwise there will be a binding error.