What is this “DataContext” you speak of?

I frequently see new WPF users confused about what the DataContext is, and how it is used. Hopefully, this can help clarify what the DataContext is, and how it is used.

What is the DataContext?

In WPF, there are two layers to an application: the UI layer and the Data layer.

The Data layer for an application starts out as null, and you can set it using the DataContext property. All UI objects will inherit their DataContext from their parent unless you specify otherwise.

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

<Label Name="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.

<Window x:Name="MyWindow" ...>

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

public partial class MyWindow: Window
    public MyWindow()
       this.DataContext = new ClassA();

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.

<Window x:Name="MyWindow" ...>
   <Label Content="{Binding Name}" />

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 -->
<Window x:Name="MyWindow"> 

    <!-- DataContext here is not specified, so it's inherited 
         from its parent's DataContext, which is ClassA -->

        <!-- DataContext inherited from parent, which is 
             ClassA, so this will display ClassA.Name -->
        <Label Content="{Binding Name}" />

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

            <!-- DataContext inherited from parent, which is 
                 ClassB, so this 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 
                       Path=DataContext.Name}" /> 

        <!-- 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 -->
        <Label Content="{Binding ClassB.Name}" />

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


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.

27 Responses to What is this “DataContext” you speak of?

  1. ohhodges says:

    Great explanation. What a convoluted system for beginners. I think I am starting to grasp the flexibility of XAML but it seems like a lot of extra steps sometimes.

  2. Kevin Golding says:

    This explanation is so much clearer than anything else I’ve found. As someone said earlier “the Microsoft docs are probably great if you already understand what they are saying”, and most other blogs just give an example without explanation.

  3. Rather Numan says:

    I came across this article through google. This is awesome

  4. Shibu Mathew says:

    Thanks a lot for this great article.

  5. David Porter says:

    I have been looking for a while. Although msdn is very thorough, it is very hard to understand unless you already understand the subject. This document is outstanding! Thanks….

  6. Great explanation..Thanks Rachel…I am still trying to get my head around WPF.This really helped

  7. Alan says:


    Thanks for a great article, but when I couldn’t to duplicate the article 😦

    I created class A & B like so

    public class ClassA
    public string Name;
    public ClassB ClassB;

    public ClassA()
    Name = “This is Class A”;
    ClassB = new ClassB();
    public class ClassB
    public string Name;

    public ClassB()
    Name = “This is Class BB”;

    Main Window is like so

    public partial class MainWindow : Window
    public MainWindow()
    this.DataContext = new ClassA();

    The top part of my MainWindow.xaml is like so

    I then pasted your code in and I expected to see
    “This is Class A” and “This is Class BB” in the appropriate places but I get a blank MainWindow.

    Are you able to tell what I have missed out? It would really help my understanding !


    • Rachel says:

      Hi Alan, WPF’s binding system only binds to propreties, not fields. Add a { get; set; } to your fields to make them properties with get/set accessors, and it should work.

  8. YousraA says:

    This is awesome! Thank you so much 😀

  9. Funmore says:

    really helpful!

  10. Natalie says:

    Great article. Very clear and concise. Thank you!

  11. Joe says:

    One of the best explanations I’ve seen on DataContext yet, very concise yet informative!

  12. Oliver says:

    Great article amongst great articles. Yours is one of the most useful sites I’ve come across since I began getting to grips with WPF. Tough content covered with laser sharp clarity. Thanks for your time and effort.

  13. Richard Bassett says:

    Rachel, you explain the concepts very well; very helpful and very much appreciated. Thank you!

  14. Matias Masso says:

    Amazing how things turn clear when somebody is kind enough to explain them. Good job,Rachel!

  15. duri says:

    Best explanation I ever seen

  16. Pranil Gatate says:

    Really Nice Post!!! Keep it up..

  17. Srini says:

    Thanks a ton.. I dint have a clearcut idea on the topic ‘DataContext’.. But your article really helped me to understand the entire thing. Great Work and very nicely drafted such that any novice can get a better understanding..

  18. Azhar says:

    Nice Post:)

  19. beyond says:

    Thank you this was helpful for me I finally understand that I’m binding to datacontext!!

  20. Jony Zaman says:

    Great post. Thank you for making it so clear. Much appreciated

  21. zar says:

    Very helpful, Thanks

  22. Dean Cunningham says:

    This was a great help, thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: