private void MeasurementLogGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            MeasurementLogEntry entry = e.Row.DataContext as MeasurementLogEntry;

            // If this entry has a valid calculator associated then change the TextBlock to a HyperlinkButton
            // that when clicked will use the Acivator object to dynamically create the appropriate calculator

            if (entry.Measurement.Calculator != null)
            {
                TextBlock measurementName = MeasurementLogGrid.Columns[0].GetCellContent(e.Row) as TextBlock;

                if (measurementName != null)
                {
                    DataGridCell    cell           = measurementName.Parent as DataGridCell;
                    HyperlinkButton calculatorLink = new HyperlinkButton();

                    calculatorLink.Content             = entry.Measurement.measurement_name;
                    calculatorLink.Margin              = new Thickness(5);
                    calculatorLink.HorizontalAlignment = HorizontalAlignment.Center;
                    calculatorLink.Click += (s, ev) =>
                    {
                        // Create an instance of the calculator using IMeasurementCalculator interface so that we have
                        // access to the calculated value and can update the selected row value

                        ChildWindow            modalWindow = new ChildWindow();
                        IMeasurementCalculator calc        = Activator.CreateInstance(Type.GetType(String.Format("FitnessTrackerPlus.Views.Measurement.Calculators.{0}",
                                                                                                                 entry.Measurement.Calculator.type_name))) as IMeasurementCalculator;

                        calc.CalculationCancelled += (se, eve) => { modalWindow.Close(); };
                        calc.CalculationComplete  += (se, eve) =>
                        {
                            modalWindow.Close();

                            entry.value = calc.CalculatedValue;
                            context.SubmitChanges();
                        };

                        modalWindow.Title   = String.Format("{0} Calculator", entry.Measurement.measurement_name);
                        modalWindow.Content = calc;
                        modalWindow.Show();
                    };

                    // Repace the TextBlock with the HyperlinkButton control

                    cell.Content = calculatorLink;
                }
            }
        }
        private void CustomMeasurementForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
        {
            if (e.EditAction == DataFormEditAction.Cancel && CustomMeasurementCancelled != null)
            {
                CustomMeasurementCancelled(this, null);
            }
            else
            {
                if (CustomMeasurementForm.ValidateItem())
                {
                    // If validation succeeds then add the exercise to the database

                    context.Measurements.Add(CustomMeasurementForm.CurrentItem as FitnessTrackerPlus.Web.Data.Measurement);
                    context.SubmitChanges((MeasurementSubmitted) =>
                    {
                        if (!MeasurementSubmitted.HasError)
                        {
                            FitnessTrackerPlus.Web.Data.Measurement customMeasurement = CustomMeasurementForm.CurrentItem as FitnessTrackerPlus.Web.Data.Measurement;

                            // If user has selected an existing unit of measure create a new entry
                            // in the measurements_measurement_units table

                            if (selectedUnit != null)
                            {
                                context.MeasurementsUnits.Add(new MeasurementsUnits {
                                    unit_id = selectedUnit.id, measurement_id = customMeasurement.id
                                });
                                context.SubmitChanges((MeasurementsUnitsSubmitted) =>
                                {
                                    if (!MeasurementsUnitsSubmitted.HasError)
                                    {
                                        if (CustomMeasurementCreated != null)
                                        {
                                            CustomMeasurementCreated(this, new CustomMeasurementCreatedEventArgs(customMeasurement));
                                        }
                                    }
                                }, null);
                            }
                            else
                            {
                                // Otherwise we also need to add an entry to the measurement_units table

                                TextBox customUnit = CustomMeasurementForm.FindNameInContent("CustomUnit") as TextBox;

                                context.MeasurementUnits.Add(new MeasurementUnit {
                                    unit = customUnit.Text, user_id = Globals.CurrentUser.id
                                });
                                context.SubmitChanges((MeasurementUnitSubmitted) =>
                                {
                                    if (!MeasurementUnitSubmitted.HasError)
                                    {
                                        // Need to use the id of the newly created unit of measure

                                        MeasurementUnit createdUnit = MeasurementUnitSubmitted.ChangeSet.AddedEntities[0] as MeasurementUnit;

                                        context.MeasurementsUnits.Add(new MeasurementsUnits {
                                            unit_id = createdUnit.id, measurement_id = customMeasurement.id
                                        });
                                        context.SubmitChanges((MeasurementsUnitsSubmitted) =>
                                        {
                                            if (!MeasurementsUnitsSubmitted.HasError)
                                            {
                                                if (CustomMeasurementCreated != null)
                                                {
                                                    CustomMeasurementCreated(this, new CustomMeasurementCreatedEventArgs(customMeasurement));
                                                }
                                            }
                                        }, null);
                                    }
                                }, null);
                            }
                        }
                    }, null);
                }
            }
        }