Search code examples
silverlightsilverlight-toolkitdataform

Is it possable to add a DataField to a Silverlight DataForm without creating an entire edit template?


I am using the DataForm for an entity with about 40 attributes. I'm happy with how the form displays all but 3 of the attributes. These 3 attributes happen to be lists of items.

I don't want to have to code out an entire edit template, seems very counter productive.

<dataFormToolkit:DataForm AutoGenerateFields="True" CurrentItem="{Binding XXX, Mode=TwoWay, Source={StaticResource XXXViewModel}}" >
                    <dataFormToolkit:DataField Label="Client"  >
                        <ListBox ItemsSource="{Binding Client}"></ListBox>
                    </dataFormToolkit:DataField>
                </dataFormToolkit:DataForm>

Solution

  • The the WCF RIA Services includes a Silverlight Business Application project template that demonstrates creating a CustomDataForm where they override OnAutoGeneratingField and modify the field for just the attributes you want. I've copied the code here for you to illustrate the idea but I'd suggest you check out the real thing to see how they are using the ReplaceTextBox extension method to deal with the Data Binding as well. Download link.

    public class CustomDataForm : DataForm
    {
        protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
        {
            // Get metadata about the property being defined
            PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);
    
            // Do the password field replacement if that is the case
            if (e.Field.Content is TextBox && this.IsPasswordProperty(propertyInfo))
            {
                e.Field.ReplaceTextBox(new PasswordBox(), PasswordBox.PasswordProperty);
            }
    
            // Keep this newly generated field accessible through the Fields property
            this.fields[e.PropertyName] = e.Field;
    
            // Call base implementation (which will call other event listeners)
            base.OnAutoGeneratingField(e);
        }
    }