Sometimes you want to display values from several individual properties combined in a single field and formatted properly. Some typical examples are a full address combined from the street address, city, state, zip code etc., a full name as a combination of the first, middle and last names, various numeric values coupled with the respective units of measurements or currency symbols and so forth.
At the same time, you don't want to have to manually update the combined value every time any of the component values changes. Also, you want to make sure that the formatted value will be displayed properly if any (or all) of the individual values are blank, e.g. not display a bunch of commas, parenthesis or other separators for an empty field. The following steps explain how you can leverage Xomega Framework to implement this type of behavior.
- Define a new data property of the type Xomega.Framework.Properties.ComboProperty on your data object. Also define a constant string for the property name.
- Construct this property during the object initialization, e.g. Initialize() method. If you're customizing a generated class, then use the OnInitialized() method instead. Pass the current data object and the property name constant to the constructor.
- Call the SetComponentProperties() method of the property and pass all the constituent properties to its parameters.
- If you want the values of each component property to be automatically trimmed, then set the TrimValues flag of the combo property to true.
- Set the Format on the combo property to the desired format string with placeholders for the component values that is used to build the combo value. The placeholders are numbers in curly braces, e.g. {0}, where the number corresponds to the index of each component property as set in the SetComponentProperties() method. If any adjacent part of the static format should not be shown when the value is blank, then these parts should be placed inside the curly braces and the number should be marked with a preceding $. For example, the placeholder { ($1)} will show the value of the second property in parenthesis, but won't show anything if that value is blank as opposed to showing empty parenthesis.
- Bind a read only text control on your form to that combo property.
The following code demonstrates how to display a combination of error procedure name followed by an optional error line in square brackets.
Code:
using System;
using Xomega.Framework;
using Xomega.Framework.Properties;
namespace MyProject.Client.Objects
{
public partial class ErrorLogObject
{
public const string ProcedureWithLine = "ProcedureWithLine";
// A formatted combination of the error procedure and the error line properties.
public ComboProperty ProcedureWithLineProperty { get; private set; }
protected override void OnInitialized()
{
// Initialize and configure any additional custom properties
// and perform other initialization tasks for the object.
// The generated properties have been initialized and configured by this point.
ProcedureWithLineProperty = new ComboProperty(this, ProcedureWithLine);
ProcedureWithLineProperty.SetComponentProperties(ErrorProcedureProperty, ErrorLineProperty);
ProcedureWithLineProperty.Format = "{0} [line: {1}]";
}
}
}