This article is currently in the process of being translated into Dutch (~15% done).

Data binding:

Using the DataContext

De DataContext eigenschap is de standaard origine van je bindingen, tenzij je specifiek een andere origine definieert, zoals wij deden in het vorige hoofdstuk met de ElementName eigenschap. Hij wordt gedefinieerd in de FrameworkElement class, zoals de meeste UI bedieningen waaronder het WPF Venster, deze erft. Om het simpel te zeggen: hij brengt je in staat een basis voor je bindingen te definiëren.

There's no default source for the DataContext property (it's simply null from the start), but since a DataContext is inherited down through the control hierarchy, you can set a DataContext for the Window itself and then use it throughout all of the child controls. Let's try illustrating that with a simple example:

<Window x:Class="WpfTutorialSamples.DataBinding.DataContextSample"
        Title="DataContextSample" Height="130" Width="280">
	<StackPanel Margin="15">
			<TextBlock Text="Window title:  " />
			<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
		<WrapPanel Margin="0,10,0,0">
			<TextBlock Text="Window dimensions: " />
			<TextBox Text="{Binding Width}" Width="50" />
			<TextBlock Text=" x " />
			<TextBox Text="{Binding Height}" Width="50" />
using System;
using System.Windows;

namespace WpfTutorialSamples.DataBinding
	public partial class DataContextSample : Window
		public DataContextSample()
			this.DataContext = this;

The Code-behind for this example only adds one line of interesting code: After the standard InitalizeComponent() call, we assign the "this" reference to the DataContext, which basically just tells the Window that we want itself to be the data context.

In the XAML, we use this fact to bind to several of the Window properties, including Title, Width and Height. Since the window has a DataContext, which is passed down to the child controls, we don't have to define a source on each of the bindings - we just use the values as if they were globally available.

Try running the example and resize the window - you will see that the dimension changes are immediately reflected in the textboxes. You can also try writing a different title in the first textbox, but you might be surprised to see that this change is not reflected immediately. Instead, you have to move the focus to another control before the change is applied. Why? Well, that's the subject for the next chapter.


Using the DataContext property is like setting the basis of all bindings down through the hierarchy of controls. This saves you the hassle of manually defining a source for each binding, and once you really start using data bindings, you will definitely appreciate the time and typing saved.

However, this doesn't mean that you have to use the same DataContext for all controls within a Window. Since each control has its own DataContext property, you can easily break the chain of inheritance and override the DataContext with a new value. This allows you to do stuff like having a global DataContext on the window and then a more local and specific DataContext on e.g. a panel holding a separate form or something along those lines.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!