1
votes

I'm experimenting with .Net RIA and Silverlight, I have a few of related entities; Client, Project and Job, a Client has many Projects, and a Project has many Jobs.

In the Silverlight app, I'm uisng a DomainDataSource, and DataForm controls to perform the CRUD operations. When a Client is selected a list of projects appears, at which point the user can add a new project for that client. I'd like to be able to fill in the value for client automatically, but there doesn't seem to be any way to do that, while there is an AddingNewItem event on the DataForm control, it seems to fire before the DataForm has an instance of the new object and I'm not sure trawling through the ChangeSet from the DomainDataSource SubmittingChanges event is the best way to do this.

I would of thought this would of been an obvious feature... anyone know the best way to achieve this functionality?

4
TheDuke, have you found the solution to your question yet? I have a similar situation: I'd like to pre-populate some DataField of my NewItem template window with some data from a XAML-defined DataContext. It could be from DataGrid or from DataForm itself. But neither that, nor any solution I tried, gives me the ability to bind the NewItem template with data.AlvinfromDiaspar
I've not had much luck with this and it was just a personal project anyway so I haven't really persued this any further. What I did in the end is by no means an ideal solution, but by using the DataForm's AddingNewItem and EditEnded events I keep a Boolean of weather the user is Adding a new item and then additionally in the EditEnded event handler I detect if the user saving a new item using that boolean and set the required values appropiatly. I havent tested this fully and my DataForms AutoEdit and AutoCommit properties are set to false.TheDuke

4 Answers

1
votes

Well, being late for the party but facing the same issue I implemented a workaround using a value converter:

public class MissingDateTimeValueConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        if (value is DateTime && (DateTime)value == DateTime.MinValue) {
            DateTime returnValue = DateTime.Now.Date;
            int addDays;
            if (!string.IsNullOrEmpty(parameter as string) && int.TryParse(parameter as string, out addDays)) {
                returnValue = returnValue.AddDays(addDays);
            }
            return returnValue;
        } else {
            return value;   
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        return value;            
    }

}

It translates missing date values (e.g. 01.01.0001) to today's date and allows adding/subtracting days using the parameter-parameter.

1
votes

Something that is commonly done is to have a screen that contains a DataGrid showing the existing data. Then have an Add button that will:

  1. Create a new item
  2. Create a ChildWindow, passing the new item to the ChildWindow constructor
  3. Have a DataForm inside the ChildWindow, bound to the item specified
1
votes

To set default values as my entity is created I added a class called [EntityName].shared.cs. I then used the technique spelled at this link. It worked well for me.

0
votes

This solution allows you to add a new item at the end of a collection. The collection is bound to DataForm. Before exiting the current handler, set the DataForm.CurrentIndex as the last item in collection then cancel the adding procedure. The new item is initialized/added and visible in Dataform ready for editing.

private void ResolutionDataForm_AddingNewItem(object sender, DataFormAddingNewItemEventArgs e)
    {
        // add a new iten in collection
        Resolution resolution = new Resolution() { FaultName = "test" };
        context.Resolutions.Add(resolution);
        //through binding the form gets updated
        ResolutionDataForm.CurrentIndex = context.Resolutions.Count-1;

        // cancel de current adding procedure
        e.Cancel = true;

    }