Walkthrough: Using a DesignInstance to Bind to Data in the Designer
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.
Note
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 Working with Settings.
Prerequisites
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
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.
In Design view, click the 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.
Note
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
Add a new class named Customer to the project. For more information, see How to: Add New Project Items.
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
Open MainWindow.xaml in the WPF Designer.
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.
xmlns:local="clr-namespace:DataBindingDemo"
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.
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
In the Properties window, scroll To the Text property.
At the edge of the left column, click the property marker ().
A menu appears.
Tip
You can also right-click the row to display the menu.
Click Apply Data Binding.
The data binding builder appears, with the Path pane open.
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
Open MainWindow.xaml.cs or MainWindow.xaml.vb in the code editor.
Replace the automatically generated MainWindow constructor with the following code.
public MainWindow() { InitializeComponent(); Customer c = new Customer(); c.FirstName = "Brenda"; c.LastName = "Diaz"; this._grid.DataContext = c; }
Press F5 to run the application.
The text box displays the first name of the Customer object that was created at run time.
Next Steps
- You can bind to sample design-time data, which helps you construct correct layout behavior at run time when your actual data is available. For more information, see Walkthrough: Using Sample Data in the WPF Designer.