This article is currently in the process of being translated into Czech (~31% done).
Using the DataContext
Vlastnost DataContext je výchozím zdrojem vazeb, pokud výslovně nedeklarujete jiný zdroj, jak tomu bylo v předchozí kapitole s vlastností ElementName. Je definován ve třídě FrameworkElement, kterou dědí většina ovládacích prvků uživatelského rozhraní, včetně okna WPF. Jednoduše řečeno, umožňuje blíže určit základ vašich vazeb.
Neexistuje výchozí zdroj pro vlastnost DataContext (na začátku má jednoduše hodnotu null), ale protože je DataContext zděděn přes hierarchii ovládacích prvků, můžete nastavit DataContext pro samotné okno formuláře a pak jej použít ve všech podřízených ovládacích prvcích. Zkusme si to ilustrovat na jednoduchém příkladu:
<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;
}
}
}

V tomto příkladu kód na pozadí přidá pouze jeden zajímavý řádek: po standardním volání InitalizeComponent () přiřadíme odkaz "this" k vlastnosti DataContext, která v podstatě pouze sděluje Oknu (Window), že chceme, aby bylo samotné datovým kontextem.
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.
Summary
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.