Silverlight 3 DataForm Control Mapping - Basic Modifications
The new DataForm control in the Silverlight Toolkit July 2009 Release does a very nice job of presenting an object in a Form view.
If you want to take more control of the default way in which the object's properties are mapped to controls, you can open up the DataForm source code and alter the behaviour.
By default, the source code is zipped up at this location upon installation:
C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Toolkit\Jul09\Source\Source code.zip
The DataForm project can be viewed by loading the RiaClient.Toolkit.sln solution file.
The methods to modify are these (in System.Windows.Controls.Data.DataForm.Toolkit/DataForm/DataForm.cs):
GetControlFromType
GetBindingPropertyFromType
If you modify the GetControlFromType method, you may need to alter GetBindingPropertyFromType to synchronise the processing.
Their original definition is as follows:
private static Control GetControlFromType(Type type){
Debug.Assert(type != null, "The type must not be null.");if (type == typeof(bool)){
return new CheckBox();}
else if (type == typeof(bool?)){
CheckBox checkBox = new CheckBox();checkBox.IsThreeState = true;return checkBox;}
else if (type == typeof(DateTime) || type == typeof(DateTime?)){
return new DatePicker();}
else if (type.IsEnum){
ComboBox comboBox = new ComboBox();FieldInfo[] valueFieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static); List<string> valueList = new List<string>();foreach (FieldInfo valueFieldInfo in valueFieldInfos){
Enum value = valueFieldInfo.GetValue(null) as Enum;if (value != null){
valueList.Add(value.ToString());
}
}
comboBox.ItemsSource = valueList;
return comboBox;}
else{
return new TextBox();}
}
private static DependencyProperty GetBindingPropertyFromType(Type type)
{
Debug.Assert(type != null, "The type must not be null.");if (type == typeof(bool) || type == typeof(bool?)){
return CheckBox.IsCheckedProperty;}
else if (type == typeof(DateTime) || type == typeof(DateTime?)){
return DatePicker.SelectedDateProperty;}
else if (type.IsEnum){
return ComboBox.SelectedItemProperty;}
else{
return TextBox.TextProperty;}
}