TOC

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

Data binding:

De DataContext Gebruiken

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.

Er is geen standaard bron voor de DataContext eigenschap (deze is gewoon null vanaf de start), maar omdat de DataContext overerft is via de control hiërarchie, kun je de DataContext voor het Window zelf instellen en dan gebruiken in alle child controls. Laten we dat illustreren met een eenvoudig voorbeeld:

<Window x:Class="WpfTutorialSamples.DataBinding.DataContextSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DataContextSample" Height="130" Width="280">
	<StackPanel Margin="15">
		<WrapPanel>
			<TextBlock Text="Window title:  " />
			<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
		</WrapPanel>
		<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" />
		</WrapPanel>
	</StackPanel>
</Window>
using System;
using System.Windows;

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

Het Code-behind bestand voor dit voorbeeld bevat een enkele regel van interessante code: na de standaard InitializeComponent() aanroep, wijzen we de "this" referentie toe aan de DataContext en dat geeft het Window in basis de opdracht dat wij willen dat het Window zelf de data context wordt.

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.

Samenvatting

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!