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 CreateMeasurementLogEntry(FitnessTrackerPlus.Web.Data.Measurement measurement) { MeasurementLogEntry entry = new MeasurementLogEntry(); // Setup new measurement log entry with selected date, measurement and current user IEnumerator units = measurement.MeasurementsUnits.GetEnumerator(); units.MoveNext(); entry.measurement_id = measurement.id; entry.entry_date = Globals.SelectedDate; entry.user_id = Globals.CurrentUser.id; entry.unit_id = (units.Current as MeasurementsUnits).unit_id; context.MeasurementLogEntries.Add(entry); context.SubmitChanges((ChangesSubmitted) => { if (!ChangesSubmitted.HasError) { MeasurementData.Load(); } }, null); }