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

StackOverflow https://stackoverflow.com/questions/2036088

Question

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>
Was it helpful?

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);
    }
}

OTHER TIPS

It will work : try that

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class IsPassword : System.Attribute { }

  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
            var attributes = propertyInfo.GetCustomAttributes(typeof(IsPassword), false).ToList();

            if (attributes.Any(obj=>obj is IsPassword))
            {
                PasswordBox box= new PasswordBox();
                Binding binding = new Binding(e.PropertyName);
                binding.Mode = BindingMode.TwoWay;
                box.SetBinding(PasswordBox.PasswordProperty, binding);
                e.Field.Content=box;            
            }
            base.OnAutoGeneratingField(e);
        }
    }

then just add [IsPassword] to your property

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top