Share via

Walkthrough: Using a DesignInstance to Bind to Data in the Designer

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

This walkthrough shows you how to use the WPF Designer for Visual Studio to create data bindings at design time for a data context that is assigned at run time. To create the data binding, you use the data binding builder to create a special design-time data context and set the DesignInstance to a business object type. DesignInstance is a design-time property.

In this walkthrough, you perform the following tasks:

  • Create the project.

  • Create a Customer class business object.

  • Data bind a TextBox control to a design-time instance of the Customer class in a data context.

When you are finished, you will have a text box that is bound at run time to a business object. The data binding is set in the WPF Designer.


The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.


You need the following components to complete this walkthrough:

  • Visual Studio 2010.

Creating the Project

The first step is to create a WPF Application project and enable the design-time properties.

To create the project

  1. Create a new WPF Application project in Visual Basic or Visual C# named DataBindingDemo. For more information, see How to: Create a New WPF Application Project.

    MainWindow.xaml opens in the WPF Designer.

  2. In Design view, click the root size tag (root size tag) at the lower-right of MainWindow to set the root size to auto size.

    In XAML view, the designer adds the d namespace mapping, which enables accessing design-time attributes like DesignHeight and DesignInstance.

Creating the Business Object

Next, create the business object. The business object is a simple Customer class that has FirstName and LastName properties.


The business object type is not required to be creatable for use in design-time data binding. For example, you can bind to an abstract class at design time.

To create the business object

  1. Add a new class named Customer to the project. For more information, see How to: Add New Project Items.

  2. Replace the automatically generated code with the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace DataBindingDemo
        public class Customer
            public string FirstName { get; set; }
            public string LastName { get; set; }

Setting the Design-time Data Context

To create data bindings by using the data binding builder, you create a special design-time data context and set the DesignInstance to the business object type.

To set the design-time data context

  1. Open MainWindow.xaml in the WPF Designer.

  2. In XAML view, add the following namespace mapping to the opening tag of MainWindow. For more information, see How to: Import a Namespace into XAML.

  3. Replace the opening tag of the Grid element with the following XAML.

    <Grid d:DataContext="{d:DesignInstance Type=local:Customer}" Name="_grid">

    This XAML establishes a design-time data context and makes the Customer class available for creating data bindings.

  4. Build the solution.

Creating the Data Binding

Now you can create data bindings to the Customer business object by using the data binding builder. The following procedure shows how to bind a TextBox control to the FirstName property of a Customer object.

To create the data binding

  1. From the Toolbox, drag a TextBox control onto the Window.

  2. In the Properties window, scroll To the Text property.

  3. At the edge of the left column, click the property marker (property marker).

    A menu appears.


    You can also right-click the row to display the menu.

  4. Click Apply Data Binding.

    The data binding builder appears, with the Path pane open.

    data binding builder

  5. Click FirstName and press Enter.

    In XAML view, Text property has a data binding to the Customer type's FirstName property.

Setting the Run-time Data Context

Finally, you assign the run-time data context. The data binding you created at design time works at run time without any changes in XAML or code.

To set the run-time data context

  1. Open MainWindow.xaml.cs or MainWindow.xaml.vb in the code editor.

  2. Replace the automatically generated MainWindow constructor with the following code.

    public MainWindow()
        Customer c = new Customer();
        c.FirstName = "Brenda";
        c.LastName = "Diaz";
        this._grid.DataContext = c;
  3. Press F5 to run the application.

    The text box displays the first name of the Customer object that was created at run time.

    data context set at run time

Next Steps

See Also




Design-time Attributes

Other Resources

Data Binding in the WPF Designer