1
votes

I'm using a Silverlight 4 dataform and attempting to use the inbuilt add button to create a new item in my collection. It works fine except that I have a number of properties that need to be set behind the scenes. I've tried hooking into the likely looking events such as AddingNewItem but the new item is readonly at that point and I can't set the properties.

Is there a trick to add new items using the Silverlight 4 dataform?

1
Can you elaborate more on properties that need to be set behind the scenes?Derek Beattie
Nothing special. Just setting the ID field on a new object to a string and the owner to the currently logged on user.sipsorcery

1 Answers

5
votes

Finally stumbled on the way to do it after much searching and trial and error.

The rather counter-intuitive place to set the properties on a newly created dataform item is in the EditEnding event handler. The dataform Mode property is readonly in the AddingNewItem handler but is equal to AddNew in the EditEnding handler.

My EditEnding handler code is along the lines of:

private void EditEnding(object sender, DataFormEditEndingEventArgs e)
{
     if (myDataForm.Mode == DataFormMode.AddNew)
     {
          MyItem item = myDataForm.CurrentItem as MyItem;
          item.ID = Guid.NewGuid().ToString();
     }
}