WPF-DataContext

来源:互联网 发布:windows截屏 编辑:程序博客网 时间:2024/06/10 18:24

看到一篇较好的关于WPF DataContext文章,原文虽是英文的,但是用词比较简单,我这种只过了四级的英语渣也能看懂,但是英语毕竟看起来不舒服,就顺手翻译了一遍,希望可以帮到看到的人,文章全文如下:

你所说的“DataContext” 到底是什么?

我经常看到一些初学WPF的人十分困惑DataContext到底是个什么东西,到底怎么用。幸运的是,这篇文章可以帮你清晰的了解DataContext,并且学会使用他。

什么是DataContext?

在WPF中, 每个应用程序都有两层: UI层和Data层.

Data层在程序初始化时是null, 你可以通过DataContext属性查看到他.所有的UI对象都会从他们的父级对象继承DataContext属性,除非你特别指定。

When using the Model-View-ViewModel (MVVM) Design Pattern, the DataContext (Data Layer) is your application, while UI objects, like Buttons, Labels, DataGrids, and even Windows, are all just user-friendly items that allow a user to easily interact with the DataContext, which is your actual application and is typically comprised of ViewModels and Models.

How is it used

Whenever you do a basic binding in WPF, you are binding to the DataContext.

For example, when you write

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

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

Other binding properties, such as ElementName or RelativeSource, can be used to tell the binding to lookup the property in something other than the current DataContext.

An Example

Lets start with a regular Window. Without setting the DataContext, the window still displays but there is no data behind it.

<Windowx:Name="MyWindow"...>
   ...
</Window>

Now suppose we set the DataContext to an object of type ClassA in the code-behind when this Window initializes:

publicpartialclassMyWindow: Window
{
    publicMyWindow()
    {
       InitializeComponent();
       this.DataContext = newClassA();
    }
}

Now the data layer behind that the Window is an object of type ClassA.

If ClassA has a property called Name, I could add a Label to the window and bind it to Name property of the DataContext, and whatever value is stored in ClassA.Name would get displayed.

<Windowx:Name="MyWindow"...>
   <LabelContent="{Binding Name}" />
</Window>

Now, suppose ClassA has a property called ClassB, and both classes have a property called Name. Here is a block of XAML which illustrates how the DataContext works. It also includes an example of how a control would refer to a property not in its own DataContext.

<!-- DataContext set to ClassA in initialization code -->
<Windowx:Name="MyWindow">
 
    <!-- DataContext here is not specified, so it's inherited
         from its parent's DataContext, which is ClassA -->
    <StackPanel>
 
        <!-- DataContext inherited from parent, which is
             ClassA, so this will display ClassA.Name -->
        <LabelContent="{Binding Name}" />
 
         <!-- DataContext is still ClassA, however we are
              setting it to ClassA.ClassB with a binding -->
        <StackPanelDataContext="{Binding ClassB}">
 
            <!-- DataContext inherited from parent, which is
                 ClassB, so this will display ClassB.Name -->
            <LabelContent="{Binding Name}" />
 
            <!-- DataContext is still ClassB, but we are
                 binding to the Window's DataContext.Name,
                 which is ClassA.Name -->
            <LabelContent="{Binding
                       ElementName=MyWindow,
                       Path=DataContext.Name}" />
        </StackPanel>
 
        <!-- We've left the StackPanel with its DataContext
             bound to ClassB, so this Label's DataContext
             is ClassA (inherited from parent StackPanel),
             and we are binding to ClassA.ClassB.Name -->
        <LabelContent="{Binding ClassB.Name}" />
    </StackPanel>
</Window>

As you can see, all the basic bindings look for their value in the data layer (DataContext) of the UI object

Summary

So to summarize, WPF applications have two layers: the UI layer and the Data layer. The data layer for an application starts out as null, and can be set using the DataContext property. UI objects without a DataContext set will inherit their data layer from their parent object. Bindings are used to look up values in the data layer, and display them in the UI layer.

When using the MVVM design pattern, the data layer is your application, while the UI layer just provides a user-friendly way to access the Data layer.