WPF DataContext StaticResource


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

This post is based on an example over at C# Corner in an article called DataBinding in WPF. As the article says, DataBinding allows for the flow of data between UI and the business model. after binding is set up, any modification done on the data in your business model will automatically be reflected in the UI, and vice versa.

The Datacontext property is used for setting the data to UI. If you do not explicitly define the source of binding, then it takes data context as default.

The Source as a Resource

Here is an example of the source just being a window resource. We have a class called Employee that we’ve defined in the code behind. We set some values in the Window.Resource and display those values through one-way binding on the User Interface.

<Window x:Class="DataBindingCSharpCorner.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:DataBindingCSharpCorner"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="DataBindingCSharpCorner" Height="250" Width="400">
    <Window.Resources>
        <local:Employee x:Key="MyEmployee" EmployeeNumber="123" FirstName="John" 
                        LastName="Doe" Department="Product Development" Title="QA Manager" />
        <local:Employee x:Key="AnotherEmployee" EmployeeNumber="129" FirstName="Mary" 
                        LastName="Smith" Department="Research" Title="Manager" />
    </Window.Resources>
    <StackPanel DataContext="{StaticResource AnotherEmployee}">
        <TextBox Text="{Binding Path=EmployeeNumber}"></TextBox>
        <TextBox Text="{Binding Path=FirstName}"></TextBox>
        <TextBox Text="{Binding Path=LastName}" />
        <TextBox Text="{Binding Path=Title}"></TextBox>
        <TextBox Text="{Binding Path=Department}" />
        <Label Margin="3">
            <Hyperlink NavigateUri="https://www.c-sharpcorner.com/blogs/databinding-in-wpf-part-i"
                       RequestNavigate="Hyperlink_RequestNavigate">
                C# Corner - DataBinding In WPF
            </Hyperlink>
        </Label>
    </StackPanel>
</Window>

Below is the code behind. We have defined our Employee class and added hyperlink functionality.

using System.Diagnostics;
using System.Windows;
using System.Windows.Navigation;
namespace DataBindingCSharpCorner
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }
    }
    public class Employee
    {
        public string EmployeeNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Title { get; set; }
        public string Department { get; set; }
    };
}
Series Navigation<< WPF the UpdateSourceTrigger PropertyWPF Types of Binding in XAML >>