views:

224

answers:

0

Im fairly new to SL3 and getting used to binding and converters.

I have a DataGrid and DataForm, one property of the Class they are bound to is a TimeSpan. Im using a Timepicker control in the form so I need to convert between DateTime from the control and TimeSpan on the object.

The converter is as follows

#region IValueConverter Members

    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        TimeSpan time = new TimeSpan();

        if (value != null)
        {
            time = (TimeSpan)value;
        }

        return time;
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        TimeSpan time = new TimeSpan();

        if (value != null)
        {
            DateTime originalValue = System.Convert.ToDateTime(value);
            time = new TimeSpan(originalValue.Hour, originalValue.Minute, 0);
        }

        return time;

    }

    #endregion

Now, if i have the Grid bound to a domain Data Source, and the form to the grids selected Item the converter works fine.

However, if i have both the Grid, and the DataForm bound to the DataSource (to allow Inserts and Deletes from the DataForm) then the ConvertBack function has a null value param when I click OK to do the Update or Insert on the DataForm.

Am i not supposed to Bind both controls to the same datasource? If not then how can I get the form to Insert and Update without having to write code? I thought multiple controls binding to the same datasource was fine as long as only one updated?

Or, am I OK to bind to both but my converter, and understanding of them, is wrong?

Thanks