WPF Tutorial – Binding without XAML

The introduction of binding and XAML has made creating user interfaces in .NET a much more enjoyable experience compared to previous incarnations. Binding is such an integral part of the WPF experience that most developers thoroughly understand how to do it in XAML, however what we’re going to look at today is how to create a binding using only C# code.

I’m going to build a very basic example application – it has a TextBlock and a TextBox. Whatever is typed into the TextBox, will be reflected in the TextBlock. Here’s a screenshot of what I’m talking about:

Example App Screenshot

This application lends itself perfectly to XAML binding, and in fact that’s the first way I implemented it.

<Window x:Class="BindingWithoutXAML.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="MainWindow"
       Height="350"
       Width="525">
  <Grid Margin="10">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="10" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <TextBox x:Name="textBox"
            VerticalAlignment="Center" />

    <TextBlock x:Name="textBlock"
              VerticalAlignment="Center"
              Grid.Column="2"
              Text="{Binding ElementName=textBox, Path=Text,
                       UpdateSourceTrigger=PropertyChanged}" />

  </Grid>
</Window>

You can clearly see my two controls and the binding that links the two. Whenever the Text property of the TextBox changes, the Text property of the TextBlock will be updated. Now let’s move the binding to the code-behind.

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
   public MainWindow()
   {
      InitializeComponent();

      // Create a binding object for the Text property of the TextBox.
      Binding binding = new Binding("Text");

      // Set the binding to update whenever the property changes.
      binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

      // Set the source object of the binding to the TextBox.
      binding.Source = textBox;

      // Add the binding to the TextBlock.
      textBlock.SetBinding(TextBlock.TextProperty, binding);
   }
}

You can definitely see the correlation between the XAML and the C#. The first thing we do is create a Binding object and tell it to bind to the Text property of its source object. We then tell the binding to update the destination property whenever the source property changes. We then give it the actual source object – the one that owns the property “Text”. Lastly we set the binding on the destination object and specify the property we’d like to bind to. If we run the application, we’d get the exact same behavior.

It’s not always obvious where you’ll need to specify bindings in the C# code, but one scenario I’ve encountered in the past is that my source object doesn’t exist right away. Sometimes I need to delay the binding until it does, and XAML doesn’t have the ability to wait until the source property exists before creating and applying the binding.

In this tutorial you’ve learned how to create bindings without using XAML. If you’ve got questions or comments, please leave them below or check out the forums.

Leave a Reply

Your email address will not be published. Required fields are marked *