Autogenerated Fields
Generally, the RadDataForm creates its fields automatically based on the type of the corresponding properties. Thus the proper editor controls are text fields for string properties, CheckBoxes for Boolean, DateTimePickers for dates, ComboBoxes for enums.
Figure 1: Autogenerated fields
However, you are free to change each of the fields displayed. What you need to do is to set the AutoGenerateFields property of the RadDataForm to True and handle the AutoGeneratingField event. As it is cancelable, you may reject the cretion of a particular field.
The arguments of the AutoGeneratingField event are:
Cancel: Gets or sets a value that indicates whether the operation should be cancelled.
-
DataField: Gets or sets the data field; it is of type DataFormDataField and depends on the type of the corresponding property. It can be:
DataFormCheckBoxField: Used for editing of boolean properties.
DataFormComboBoxField: Used for editing by choosing a value from predefined data source.
DataFormDataField: Used for editing of string properties.
DataFormDateField: Used for editing properties of type DateTime.
PropertyName: Gets or sets the property name of the corresponding field.
PropertyType: Gets or sets the property type of the corresponding field.
The AutoGeneratingField event will be fired for each current item, thus enabling you to customize the view on item basis.
So, for example, let us decide on defining a Description for the Salary property, set the Label of the first property to be "First Name" and define purple foreground color for the ladies and a blue one for the gentlemen:
Example 1: Handling the AutoGeneratingField Event
private void DataForm1_AutoGeneratingField(object sender, Telerik.Windows.Controls.Data.DataForm.AutoGeneratingFieldEventArgs e)
{
var employee = this.DataForm1.CurrentItem as Employee;
if (e.PropertyName == "FirstName")
{
e.DataField.Label = "First Name";
}
if (e.PropertyName == "Salary")
{
e.DataField.Description = "This is the initial salary!";
}
if (employee.Gender == Gender.Female)
{
e.DataField.Foreground = new SolidColorBrush(Colors.Purple);
}
else
{
e.DataField.Foreground = new SolidColorBrush(Colors.Blue);
}
}
Public Sub DataForm1_AutoGeneratingField(sender As Object, e As Telerik.Windows.Controls.Data.DataForm.AutoGeneratingFieldEventArgs)
Dim employee = TryCast(Me.DataForm1.CurrentItem, Employee)
If e.PropertyName = "FirstName" Then
e.DataField.Label = "First Name"
End If
If e.PropertyName = "Salary" Then
e.DataField.Description = "This is the initial salary!"
End If
If employee.Gender = Gender.Female Then
e.DataField.Foreground = New SolidColorBrush(Colors.Purple)
Else
e.DataField.Foreground = New SolidColorBrush(Colors.Blue)
End If
End Sub
Figure 2: Result of customizing the autogenerated fields
DataFormDateField EditorMode
The DataFormDateField is used for editing of DateTime properties. The default property editor is a control of type RadDatePicker. You can change this and use RadDateTimePicker or RadTimePicker by setting the EditorMode property.
The EditorMode property expects the following values:
- Date: RadDatePicker control will be used as a property editor .
- Time: RadTimePicker control will be used as a property editor.
- DateTime: RadDateTimePicker control will be used as a property editor.
Example 2: Setting the EditorMode
private void RadDataForm_AutoGeneratingField(object sender, AutoGeneratingFieldEventArgs e)
{
if (e.DataField is DataFormDateField)
{
(e.DataField as DataFormDateField).EditorMode = DateFieldEditorMode.Time;
}
}
Private Sub RadDataForm_AutoGeneratingField(ByVal sender As Object, ByVal e As AutoGeneratingFieldEventArgs)
If TypeOf e.DataField Is DataFormDateField Then
(TryCast(e.DataField, DataFormDateField)).EditorMode = DateFieldEditorMode.Time
End If
End Sub