You can specify a binding for a WPF control to a data property of a Xomega data object directly in the XAML. The data object serves as a view model for your form. When the object is set as the DataContext for the view, the control will be automatically bound to the corresponding data property, which will allow Xomega Framework to keep the content of the control (such as the value or a list of values) as well as the control's state (e.g. visibility, editability, etc.) in sync with that of the data property.
In order to specify a property binding on a control, you can perform the following steps.
- Declare a namespace xom on your window in the XAML that maps to the Xomega Framework namespace, e.g. xmlns:xom="clr-namespace:Xomega.Framework;assembly=Xomega.Framework"
- Add an attribute xom:Property.Name to the XML element of the control and set its value to the name of the corresponding data property in the data object. To allow compiler validations after renaming a data property, you can bind the attribute value to a static string constant declared on your data object for the property name using the x:Static construct, for example xom:Property.Name="{x:Static l:ContactObject.FirstName}". This example assumes that you have the prefix l mapped to the namespace of your data object, e.g. xmlns:l="clr-namespace:AdventureWorks.Client.Objects;assembly=AdventureWorks.Client.Objects".
- If you want to bind to a data property that is defined on a child object of the current context object, then you can set the xom:Property.ChildObject attribute to indicate a child object's name or a dot-delimited path to it. As described above, you can bind it to a static string constant to allow compiler checks after refactoring.
- Optionally set the xom:Property.Label attribute to indicate the label element associated with this control. This will allow Xomega Framework to show or hide the label along with the control and possibly highlight the label for required properties.
- Make sure that in your code behind you set the DataContext of the encapsulating panel to your data object, e.g. pnlMain.DataContext = obj;
The following XAML snippet demonstrates these steps.
Code:
<Label Name="lblFirstName" Grid.Row="5" Grid.Column="2">_First Name:</Label>
<TextBox Grid.Row="5" Grid.Column="3" Name="ctlFirstName"
xom:Property.Name="{x:Static l:ContactObject.FirstName}"
xom:Property.ChildObject="{x:Static l:EmployeeObject.Contact}"
xom:Property.Label="{Binding ElementName=lblFirstName}"/>