Silverlight DataForm Auto Generation Horror!

Today I was trying to use automatic generation of DataForm from my ViewModels.

After an hour of exploring it I recognized that it is nightmare!

Look at this method:

   1: private static Control GetControlFromType(Type type)
   2: {
   3:     Debug.Assert(type != null, "The type must not be null.");
   4:  
   5:     if (type == typeof(bool))
   6:     {
   7:         return new CheckBox();
   8:     }
   9:     else if (type == typeof(bool?))
  10:     {
  11:         CheckBox checkBox = new CheckBox();
  12:         checkBox.IsThreeState = true;
  13:         return checkBox;
  14:     }
  15:     else if (type == typeof(DateTime) || type == typeof(DateTime?))
  16:     {
  17:         return new DatePicker();
  18:     }
  19:     else if (type.IsEnum)
  20:     {
  21:         ComboBox comboBox = new ComboBox();
  22:  
  23:         FieldInfo[] valueFieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static);
  24:         List<string> valueList = new List<string>();
  25:  
  26:         foreach (FieldInfo valueFieldInfo in valueFieldInfos)
  27:         {
  28:             Enum value = valueFieldInfo.GetValue(null) as Enum;
  29:  
  30:             if (value != null)
  31:             {
  32:                 valueList.Add(value.ToString());
  33:             }
  34:         }
  35:  
  36:         comboBox.ItemsSource = valueList;
  37:         return comboBox;
  38:     }
  39:     else
  40:     {
  41:         return new TextBox();
  42:     }
  43: }

You see that HARDCODED controls? It is terrible mistake.

What if I want to use Telerik controls?

What if I want to have NumericUpDown for int field?

Also I often need different editors for the same value type.

IMHO  the right solution is to set up generated control with some custom annotation of model properties.

Perhaps the main reason to use DataForm is its auto generation. But it is now totally unusable even for simple models.

Already released DataGrid has the same issue too.

Also I think that edit, add, submit button should not present in DataForm control at all.

PS: The only way is to generate datafield properly is overriding of OnAutoGeneratingField, but it is HACK!

1 Comment

Comments have been disabled for this content.