Data Binding WPF Controls
Unlike Windows Forms and Web Forms technologies, MM .NET does not require special subclasses of controls to work with WPF. By taking advantage of advanced technologies in WPF such as attached properties, all logic for data binding, localization, and security is found at the Window or Page level. This allows you to use standard WPF controls in MM .NET WPF applications.
WPF Data Binding Primer
In WPF data binding, there is a source and a target. The source in the case of MM .NET is usually a strongly typed entity object or an mmBindingList of entity objects, although at times the source can be another user interface element (when you need to sync one user interface element with another). The target is always a dependency property on a user interface element.The following table shows the different types of data binding available in WPF:
| Type | Description |
|---|---|
| OneWay | The target property is updated when the source property changes |
| TwoWay | The target property is updated when the source property changes, and the source property is updated when the target property changes |
| OneTime | The target property is set initially based on the source property value. Changes are ignored from that point on unless the binding is set to a completely different object or you call BindingExpression.UpdateTarget(). Most often used when you know the source property will not change |
| OneWayToSource | Similar to OneWay but in reverse. The source property is updated when the target property changes, but the target property is never updated. This is most often used when data binding a property that is not a dependency property |
| Default | The type of binding depends on the target property. It's either TwoWay (for user-settable properties such as TextBox.Text) and OneWay for everything else. This is the default setting |
The timing of the bind back (when data is bound back from the user interface control to the data source) is determined by the UpdateSourceTrigger property of the user interface element:
| Name | Description |
|---|---|
| PropertyChanged | The source is updated immediately when the target property changes |
| LostFocus | The source is updated when the target property changes and the target loses focus |
| Explicit | The source is not updated unless you call the BindingExpression.UpdateSource() method |
| Default | The updating behavior is determined by the metadata of the target property. For most properties, the default is PropertyChanged, although TextBox.Text default is LostFocus |
When using the Explicit option, you need to get the UI element's BindingExpression object (an object that wraps the Binding object (BindingExpression.ParentBinding) and the object that's being bound from the source (BindingExpression.DataItem). Here's how you get a BindingExpression object from any user interface element:
// Get the binding that's applied to the text box
BindingExpression binding = txtFontSize.GetBindingExpression(TextBox.TextProperty);
// Update the source (option 1)
binding.UpdateSource();
// Update the target (option 2)
binding.UpdateTarget();Binding to MM .NET Entity Objects
When binding to an object that isn't a WPF element, such as an MM .NET entity object, behind the scenes the following binding properties of the user interface element are used:
| Property | Description |
|---|---|
| Source | This is a reference that points to the source object-the object that is supplying the data |
| RelativeSource | This points to the source object using a RelativeSource object which allows you to base your reference on the current element. This is a specialized tool that's handy when writing control templates and data templates |
| DataContext | If you don't specify a source using the Source or RelativeSource property, WPF searches up the element tree starting at the current element. It examines the DataContext property of each element and uses the first one that isn't null. The DataContext property is extremely useful if you need to bind several properties of the same object to different elements, because you can set the DataContext property of a higher-level container object rather than directly on the target element. |
The mmBusinessEntity class implements the INotifyPropertyChanged interface. Entity objects generated by the Business Layer Generator make use of this new interface by calling the OnPropertyChanged() method in the Set() of all entity properties. This ensures that if you manually store a value to an entity object, any user interface element bound to it is automatically updated.
Step 1: Creating a Data Context
As mentioned above, all WPF user interface elements possess a DataContext property. If you set the DataContext property of a user interface container such as a Window, Page, Tab, TabItem, Grid, or other layout panel, all controls within the container automatically point to this DataContext, unless their own Source property is set to another data source.
This means you should find the highest level container that contains controls that are bound to the same entity object and set its DataContext property to the appropriate entity.
For example, if most controls on a window will be bound to an OrderEntity, it makes sense to assign OrderEntity to the Window's DataContext property as outlined in the following steps.
- Select the Window's .xaml file in the Solution Explorer.
- In the Document Outline window, click on the top Window node.
- In the Properties panel under the Common section, find the DataContext property and click the New button next to it. This displays the Select Object dialog. Expand the namespace node and then select OrderEntity and click OK:
After clicking OK, the new Data Context is displayed in the Properties panel:
With this set, Visual Studio shows OrderEntity as the Explicit Data Context for all user interface elements on the window.
Step 2: Data Binding Individual User Interface Elements
Before binding individual user interface elements, make sure you have set up one or more entity objects as a data context as described in the section above. Afterwards, you can set the UI element's binding properties. For example, to bind an Order ID text box to an OrderEntity's OrderID property, you could do this:
- In Visual Studio with a WPF window open in the design surface, select the window's Properties tab, then select the txtOrderID text box. In the Properties panel under Common Properties, select the tiny Advanced property options button to the right of the Text property, then select Create Data Binding... from the shortcut menu.
- In the Create Data Binding dialog, the Binding Type should be set toData Context . On the right side of the dialog in the Path box, select OrderID and click OK:

Afterwards, an amber ellipse appears around the Text property indicating it is data bound:

This only specifies the name of the entity property to bind the user interface element. You need to follow steps in the next section to instantiate a business object at run time and retrieve an entity or list of entities to bind to as described in the next section.
Step 3: Retrieving Entities at Run Time
The final step required for data binding is to add code to your window that retrieves an entity or entity list at run time to which the user interface elements will be bound. Since you are creating code, this needs to be done in Visual Studio. If your project is not already open in Visual Studio, you can go to Expression Blend's Project palette, right-click the solution or project and select Edit in Visual Studio from the shortcut menu. You can then add code to your window or page that instantiates the business object then sets the data context you specified in step 1 to an actual entity object.
For example, in C#:
this.Window.DataContext = this.Employee.GetEmployeeByID(employeeID);And in VB .NET:
Me.Window.DataContext = Me.Employee.GetEmployeeByID(employeeID)For more details on using business objects with WPF windows and pages, check out the topics Using Business Objects with WPF and Registering Business Objects with WPF.
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/26/18
Comment or report problem with topic
