While working on a new work item template (WIT) for Team Foundation Server 2010 I encountered a requirement for a field where the user could select the time a call was received. The DateTimeControl for TFS work items displays only a date picker by default, which for this particular use case was not quite accurate enough.
My first reaction was to start building a custom work item control – but with a bit of digging into the existing one using Reflector I discovered that the ability to change the control format was indeed supported, just not immediately obvious.
It turns out that for any implementation of IWorkItemControl, the Properties property is set to contain all the attributes on the controls declaration line within the WIT. For example, the following control declaration will result in four entries within the Properties collection (FieldName, Type, Label and LabelPosition).
Additional attributes can be added to the control without invalidating the schema, so work item controls can make use of this feature to support additional configuration parameters. The DateTimeControl includes the following properties that can be set using this mechanism:
- Format: A valid selection from the DateTimePickerFormat enumeration (Long, Short, Time, Custom)
- CustomFormat: If the Format is set to Custom then the CustomFormat property is used to define the display format. This uses the standard formatting strings for .NET date formats.
Thus the following control definition can be used to add a Time-aware picker to a work item.
CustomFormat=“dd/MM/yyyy hh:mm:ss“ />
For anyone that prefers to use the WIT editor within the TFS Power Tools (found here), these properties can be added using the String Collection Editor associated with the Attributes property for the DateTimeControl.
As an added bonus, the control is rendered in correctly in both Team Explorer and Team Web Access views of the work item. Unfortunately it is not possible to change the DateTime selection to use the spin control (aka the up-down control) rather than the calendar drop down, but that is a relatively minor problem.