WPF DataContext Targeting the Main Window


This entry is part 3 of 7 in the series WPF Data Binding

The DataContext property is the default source of your bindings unless you specifically declare another source. In the previous example, we used the ElementName property to point to the TextBox (the source). Since we specified the source to be the TextBox, the default property DataContext was not in play. There’s no default source for the DataContext property (it’s simply null from the start).

We can set a DataContext for the Window itself and then use it throughout all of the child controls. The code-behind for this example only adds one line of interesting code: We assign the “this” reference to the DataContext, which basically just tells the Window that we want itself to be the data context.

Run the program and try writing a different window title in the first textbox. This change is not reflected immediately. Instead, you have to move the focus to another control before the change is applied.

Below is the XAML code.

<Window x:Class="DataContext.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataContext"
        mc:Ignorable="d"
        Topmost="False"
        FontSize="12" FontWeight="Normal"
        WindowStartupLocation="CenterScreen"
        Title="DataContext" Height="250" Width="400" MinHeight="220" MinWidth="310">
    <StackPanel Margin="15">
        <WrapPanel Margin="0,10,0,0">
            <TextBlock Text="Font Size "/>
            <TextBox Text="{Binding Path=FontSize}" Width="50"/>
        </WrapPanel>
        <WrapPanel Margin="0,10,0,0">
            <TextBlock Text="Font Weight "/>
            <TextBox Text="{Binding Path=FontWeight}" Width="80"/>
        </WrapPanel>
        <WrapPanel Margin="0,10,0,0">
            <TextBlock Text="Window title:  " />
            <TextBox Text="{Binding Path=Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
        </WrapPanel>
        <WrapPanel Margin="0,10,0,0">
            <TextBlock Text="Window dimensions (w x h): " />
            <TextBox Text="{Binding Path=Width}" Width="50" />
            <TextBlock Text=" x " />
            <TextBox Text="{Binding Height}" Width="50" /> <!-- Path is default -->
        </WrapPanel>
        <WrapPanel Margin="0,10,0,0">
            <TextBlock Text="Always on Top? "/>
            <TextBox Text="{Binding Path=Topmost}" Width="50"/>
        </WrapPanel>
    </StackPanel>
</Window>

Notice that Path is the default. Therefore it is optional. That is why Text=”{Binding Height}” works, instead of Text=”{Binding Path=Height}”. Below is the C# code.

using System.Windows;

namespace DataContext
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
    }
}
Series Navigation<< WPF Data Binding in XAMLWPF Data Binding in Code Behind >>