views:

929

answers:

3

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>
A: 

I'm pretty sure it's not possible. If I were you I would swallow my grief and create that edit template.

The only alternative I can see is to work with the data in your viewmodel and create a separate class that holds the 37 properties that need no changing. Then you make a separate entity for the 3 that need special attention. This way you could have two data forms, one autogenerated and one custom. Hopefully you can then work with styling them so they look like one form. A lot of work, I know, but it might be even more work to create the full edit template.

Henrik Söderlund
+3  A: 

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);
    }
}
Martin Hollingsworth
A: 

It is possible for sure, but it's unfortunate that there is no documentation about it. Where can I find information about DataField class used by the Field property? I don't want to see a 2h video about DataForms. I just want to see DataField class diagram. Any luck?

Zorayr