DataContext 有什么用? [英] What is DataContext for?

查看:42
本文介绍了DataContext 有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为问题的延续 将 DataContext 与 WPF 中的另一个属性链接.

在研究的最后,我很惊讶地发现当有人写这样的东西时:

At the very end of the research I was very surprised to find out that when one writes something like that:

<Label Content="{Binding Path=Name}" />

绑定Content 属性的DataContext 属于Label 控件本身!它仍然有效的事实是由于 DataContext 值默认继承自最近的父级.

The DataContext against which the Content property is binded is of the Label control itself! The fact that it still works is due to the default inheritance of the DataContext value from the nearest parent.

但是如果您将此标签包装在自定义控件中,并且您不想将数据绑定到该控件的 DataContext 属性,那么您可能更喜欢:

But if you have this label wrapped in a custom control, and you don't want to bind your data to the DataContext property of that control, you would more likely love to have:

<Controls:SearchSettings Settings="{Binding Path=Settings}" />

你来了.现在您需要将 Settings 设置为 SearchSettings 控件的 DataContext,以便 Label 在里面绑定,但是你不能,因为这会触发 Settings 属性的重新绑定.

And here you are. Now you need to set Settings as the DataContext for the SearchSettings control, for Label inside to bind against, but you can't, because that will trigger re-binding of Settings property.

我看不出混合使用不同来源的绑定属性的意义:DataContextElementName 等.那么我为什么要使用 DataContext?

I can't see the point in mixing binding properties using different sources: DataContext, by ElementName, etc. So why would I ever use DataContext?

推荐答案

当你写作时

<Label name="myLabel" Content="{Binding Path=Name}" />

您绑定的是 myLabel.DataContext.Name,而不是 myLabel.Name.

you are binding to myLabel.DataContext.Name, and not myLabel.Name.

WPF 中的 XAML 只是一个漂亮的用户界面,用于显示实际数据并与之交互,也称为 DataContext.其他绑定源(RelativeSourceElementName 等)的目的是指向当前控件的DataContext 中不存在的另一个属性<小时>所以假设你有一个窗口.不设置DataContext,窗口仍然显示,但后面没有数据.

The XAML in WPF is just a pretty user interface to display and interact with the actual data, otherwise known as the DataContext. The purpose of other binding sources (RelativeSource, ElementName, etc) is to point to another property that doesn't exist in the current control's DataContext


So suppose you have a Window. Without setting the DataContext, the window still displays but there is no data behind it.

现在假设设置 myWindow.DataContext = new ClassA();.现在窗口显示的数据是ClassA.如果 ClassA 有一个名为 Name 的属性,我可以编写一个标签并将其绑定到 Name(例如您的示例),以及任何值存储在 ClassA.Name 中将被显示.

Now suppose to set myWindow.DataContext = new ClassA();. Now the data that the window is displaying is ClassA. If ClassA has a property called Name, I could write a label and bind it to Name (such as your example), and whatever value is stored in ClassA.Name would get displayed.

现在,假设ClassA 有一个ClassB 的属性,并且两个类都有一个名为Name 的属性.这是一个 XAML 块,它说明了 DataContext 的用途,以及一个控件如何引用不在它自己的 DataContext 中的属性的示例

Now, suppose ClassA has a property of ClassB and both classes have a property called Name. Here is a block of XAML which illustrates the purpose of the DataContext, and an example of how a control would refer to a property not in it's own DataContext

<Window x:Name="myWindow"> <!-- DataContext is set to ClassA -->
    <StackPanel> <!-- DataContext is set to ClassA -->

        <!-- DataContext is set to ClassA, so will display ClassA.Name -->
        <Label Content="{Binding Name}" />

         <!-- DataContext is still ClassA, however we are setting it to ClassA.ClassB -->
        <StackPanel DataContext="{Binding ClassB}">

            <!-- DataContext is set to ClassB, so will display ClassB.Name -->
            <Label Content="{Binding Name}" />

            <!-- DataContext is still ClassB, but we are binding to the Window's DataContext.Name which is ClassA.Name -->
            <Label Content="{Binding ElementName=myWindow, Path=DataContext.Name}" /> 
        </StackPanel>
    </StackPanel>
</Window>

如您所见,DataContext 基于 UI 对象背后的任何数据.

As you can see, the DataContext is based on whatever data is behind the UI object.

更新:我经常从新的 WPF 用户那里看到这个问题,因此我将此答案扩展到我博客上的一篇文章中:你说的这个DataContext"是什么?

Update: I see this question so often from new WPF users that I expanded this answer into a post on my blog: What is this "DataContext" you speak of?

这篇关于DataContext 有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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