views:

782

answers:

2

We are investigating how to create data entry views from a dynamic list of pre-defined field definitions. By "pre-defined", I mean that there are only 8 basic field types. The Silverlight Toolkit's DataForm control is almost what want, but it targets object properties (not a list of custom definitions).

Is there an existing project to make this easy? Please comment on my design idea (below). I have only ~2 weeks Silverlight experience.

Basic design idea:

I am thinking of defining custom data field types. An IEnumerable<BaseDataField> will be received by the UI, enumerated, and controls will be created based on the type of each field. Each field will create a label with the description and BooleanDataField will create a CheckBox, LookupDataField will create a ComboBox, etc.

Pseudo code to clarify the idea:

public abstract class BaseDataField {
    public string FieldCode { get; private set; }
    public string FieldDescription { get { return FieldDefinitions.Instance.FieldDescription(FieldCode); } }
    ...
}

public class StringDataField : BaseDataField
public class BooleanDataField : BaseDataField
public class CurrencyDataField : BaseDataField
public class IntegerDataField : BaseDataField
public class NumericDataField : BaseDataField
public class DateTimeDataField : BaseDataField
public class LookupDataField : BaseDataField
public class SpecialDataField : BaseDataField

This will be extended to make the fields bindable; allow specifying custom controls for each type; and have validation feedback.

Can it be easily done in Silverlight or should we create a custom control?

Note: This programme will be a web UI for an existing, multi-tier LOB platform. All data is serialised from a JSON-based REST service.

Thanks!

+2  A: 

I would highly recommend incorporating the DataForm class into your solution and it is indeed capable of handling custom controls. I've been chasing this goal of an ideal minimal-xaml data form for years and I have finally settled on what I think is a great combination of the built-in functionality of DataForm and custom fields derived from DataField. For example, I just extended DataField so that when no Content property is specified, it automatically uses a TextBox which minimized my XAML big time. I also added support for the DisplayFormatAttribute which DataForm doesn't seem to support natively.

Anyhow, what you could do first is create a bunch of subclasses like you describe above and derive from DataField. Then in the OnApplyTemplate method, create an element to represent the data. For example:

class BooleanDataField : DataField {

    protected override void OnApplyTemplate() {

        if (this.Content == null) {
            var check = new CheckBox();
            check.SetBinding(CheckBox.IsCheckedProperty, 
                new Binding(this.PropertyPath));
            this.Content = check;
        }

        base.OnApplyTemplate();
    }

}

As for how to get your field metadata loaded by DataForm, you have a few options. You could handle the AutoGeneratingField event and look up the appropriate field to use on the fly or you can disable auto field generation and just load it up with your own.

Josh Einstein
Is it possible to use auto generation for a dynamic list of fields? We have a tool allowing users to create and link custom fields to products. DataForm.GenerateFields() seems to only generate fields from complex type properties.Thanks for your recommendation. I will investigate manual DataForm population w/ binding and mark your answer as accepted if it works.
carlmon
This option looks very interesting, I would love to see how it would all fit together.
Grayson Mitchell
A: 

We started with DataForm, but ended up overriding everything that makes it useful and still had some problems. I learned a great deal from the attempt and eventually built a custom control for the job. Unfortunately this solution is not very extensible...

carlmon