为什么将窗口的 DataContext 设置为 {BindingrelativeSource={RelativeSource Self}} 允许您将数据绑定到代码隐藏的属性?

发布于 2025-01-18 13:57:28 字数 394 浏览 3 评论 0原文

根据我的理解,将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 技术交流群。

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

发布评论

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

评论(1

从来不烧饼 2025-01-25 13:57:28

是的,将控件的 DataContext 设置为自身允许您在 Binding 中访问其属性。

  • 隐藏代码

    public MyWindow()
    {
       数据上下文=这个;
       初始化组件();
    }
    

    在 XAML 中创建绑定将自动继承此 DataContext 作为绑定的 Source(如果不是)覆盖,例如将项目设置为 ItemsControl 中的 DataContext

    <块引用>

    数据上下文是一个概念,它允许元素从其父元素继承有关用于绑定的数据源的信息,以及绑定的其他特征(例如路径)。 [...]

  • XAML

    <窗口 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            x:Class="MyWpfApp.MyWindow"
         ...
            DataContext =“{绑定RelativeSource = {RelativeSource Self}}”>
    

    RelativeSource 会将 BindingSource 设置为窗口本身。如果您使用 AncestorType={x:Type Window},情况也是如此。

    <块引用>

    通过指定绑定源相对于绑定目标位置的位置来获取或设置绑定源。

    无法在单个绑定上设置多个不同的源

    <块引用>

    RelativeSource:与 ElementNameSource 互斥;

本质上,如果不设置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 a Binding.

  • Code-Behind

    public MyWindow()
    {
       DataContext = this;
       InitializeComponent();
    }
    

    Creating a Binding in XAML will automatically inherit this DataContext as Source for binding if it is not overridden e.g. an item is set as DataContext in an ItemsControl.

    Data context is a concept that allows elements to inherit information from their parent elements about the data source that is used for binding, as well as other characteristics of the binding, such as the path. [...]

  • XAML

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            x:Class="MyWpfApp.MyWindow"
         ...
            DataContext="{Binding RelativeSource={RelativeSource Self}}">
    

    The RelativeSource will set the Source of the Binding to the window itself. The same would be true, if you would use AncestorType={x:Type Window}.

    Gets or sets the binding source by specifying its location relative to the position of the binding target.

    You cannot set multiple different sources on a single binding.

    RelativeSource: mutually exclusive versus with ElementName and Source;

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 a Window as source? You have to understand, that the XAML file and your code-behind are one and the same class after compilation. They are both partial definitions that are merged on compilation. You are right that you inherit from Window, but your understanding of bindings is wrong. If we talk about class inheritance, you are right, accessing a derived MyWindow through a Window 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 the DataContext, which is MyWindow.

Defers a property value to be a data-bound value, creating an intermediate expression object and interpreting the data context that applies to the element and its binding at run time.

You could assign and switch any arbitrary object as DataContext even of different types. As long as the Path of any binding matches any property value it will be resolved successfully, otherwise there will be a binding error.

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