1

I'm having a dataform which is binded to a property in my view-model in a Silverlight application, I've created my entity classes with WCF RIA Services and every property has the attribute of DisplayName which is shown in the dataform datafield label. what I need to do is to add a ":" at the end of every label in the custom datafields that I create. The reason I need this to happen is because I have a grid in my page which is binded to the list of current objects (e.g. Employees) and I don't want ":" at the end of the grid headers, but I also need ":" when I'm trying to edit or add a new employee.

This is what I've done so far, but it's not working.

public class CustomDataField : DataField
{
    public CustomDataField()
    {

    }

    public new object Label
    {
        get { return base.Label; }
        set 
        { 
            base.Label = value;
            if( value is string )
            {
                base.Label = (string)value + ":";
            }
        }
    }
}
Peymankh
  • 1,976
  • 25
  • 30

1 Answers1

1

(1)

When you don't let the DataForm autogenerate the fields, you have more control over the fields and can set the labels manually:

<tkt:DataForm AutoGenerateFields="False" AutoEdit="True">
    <tkt:DataForm.EditTemplate>
        <DataTemplate>
            <StackPanel>
                <tkt:DataField Label="SomeLabel:">
                    <TextBox Text="{Binding SomeProperty, Mode=TwoWay}" />
                </tkt:DataField>

                [...]

            </StackPanel>
        </DataTemplate>
    </tkt:DataForm.EditTemplate>
</tkt:DataForm>

(2)

If you need the auto-generating functionality, but you also need more control over how fields are displayed, you could wrap the DataForm into your own custom control. You'll have to implement the auto-generation yourself to build your own EditTemplate, which you'd assign to the DataForm. This is the road that I took.

(3)

Another quick and dirty way would be to iterate through the visual tree after the DataForm has rendered to change the labels. That goes pretty straightforward with a little help from the toolkit:

// needs System.Windows.Controls.Toolkit.dll

using System.Linq;
using System.Windows.Controls.Primitives;

foreach (var field in myDataForm.GetVisualDescendents().OfType<DataField>())
{
    field.Label = field.Label + ":";
}

(4)

Finally, I just saw that there is an AutoGeneratingField event on the DataForm that could work (untested):

myDataForm.AutoGeneratingField += (sender, e) => e.Field.Label = e.Field.Label + ":";
herzmeister
  • 11,101
  • 2
  • 41
  • 51
  • That's exactly the point, I don't want to use AutoGeneratingField and also, as you know, when you bind a control (e.g. a TextBox) in your datafield, the label is set automatically (from data entity DisplayName attribute), that I need to be able to edit. not set the label with bare hands ;) The first thing that came to my mind was, create a class and inherit from the base and shadow the label property but since Label is never set, you can't change the value. I need to know when DataField sets the Label property. – Peymankh Jan 08 '11 at 16:32
  • @Peymankh Only my first proposal dealt with manually creating DataFields. Did you try the other three? Those are general and reusable. You'll get access to the generated DataField. You might need to add a listener for the Label property though. How to do that, take a look here: http://stackoverflow.com/questions/834929/silverlight-how-to-receive-notification-of-a-change-in-an-inherited-dependencypr – herzmeister Jan 08 '11 at 16:53
  • I think inheriting the DataField like you tried is not so easily possible, or have you found a way to tell the DataForm to create instances of your CustomDataField class instead of the normal DataField class? That said, another option would be to change the source code directly. The Toolkit is open source after all, so just link the project with the source. – herzmeister Jan 08 '11 at 16:58
  • @herzmejster The second and the third option won't be so likely to implement and as you said, it's a dirty code, and the 4th one, since AutoGeneratingField is set to false, you won't get an event for it. – Peymankh Jan 08 '11 at 17:05
  • I tried the CustomDataField class and it works well as expected. Maybe I look at the Toolkit source code and see if I can take something out of it. but meanwhile I'll try to find another way to get where I want. – Peymankh Jan 08 '11 at 17:07