private void addFormElement() { if ((ChosenFormElementOption == null) || (ChosenFormElementOption == String.Empty)) { return; } if (ChosenFormElementOption == "Label") { LabelFormElement e = new LabelFormElement(); e.Name = "New Label"; e.LabelText = "New Label"; LabelElementWrapper w = new LabelElementWrapper(e); _template.FormElements.Add(e); FormElements.Add(w); generateInstance(); } else if (ChosenFormElementOption == "Text Box") { TextFormElement e = new TextFormElement(); e.Name = "New text box"; FormElementWrapper w = new FormElementWrapper(e); _template.FormElements.Add(e); FormElements.Add(w); generateInstance(); } else if (ChosenFormElementOption == "Numeric Up Down") { NumericFormElement e = new NumericFormElement(); e.Name = "New numeric box"; NumericElementWrapper w = new NumericElementWrapper(e); _template.FormElements.Add(e); FormElements.Add(w); generateInstance(); } }
private void FormViewer_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (!(this.DataContext is FormInstance)) { return; } ///Ok so the datacontex wil be a Forminstance FormInstance f = (FormInstance)this.DataContext; //does this forminstance already have dataitems //We clear all the existing elements from the Grid FormGrid.Children.Clear(); //Set up the rows and columns FormGrid.RowDefinitions.Clear(); FormGrid.ColumnDefinitions.Clear(); var rows = f.FormTemplate.Rows.Split(','); for (int i = 0; i < f.FormTemplate.NumberOfRows; i++) { RowDefinition def = new RowDefinition(); Binding widthBinding = new Binding("ActualHeight"); widthBinding.Source = FormGrid; widthBinding.Converter = new FractionConverter(); widthBinding.ConverterParameter = Convert.ToDouble(rows[i]); def.SetBinding(RowDefinition.HeightProperty, widthBinding); FormGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(Convert.ToDouble(rows[i]), GridUnitType.Star) }); } var cols = f.FormTemplate.Columns.Split(','); for (int i = 0; i < f.FormTemplate.NumberOfColumns; i++) { ColumnDefinition def = new ColumnDefinition(); Binding widthBinding = new Binding("ActualWidth"); widthBinding.Source = FormGrid; widthBinding.Converter = new FractionConverter(); widthBinding.ConverterParameter = Convert.ToDouble(cols[i]); def.SetBinding(ColumnDefinition.WidthProperty, widthBinding); //FormGrid.ColumnDefinitions.Add(def); FormGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(Convert.ToDouble(cols[i]), GridUnitType.Star) }); } ///Now we iterate the elements that make up this template ///and add them to the grid ///for each form element, we add a control, and we check if the FormInstance already has a ///QADataItem that matches in terms of its DataDictionaryEntry and EquipmentItem ///if we have match, then we assign that dataitem to the Control we just added foreach (FormElement elm in f.FormTemplate.FormElements) { if (elm is LabelFormElement) { LabelFormElement l = (LabelFormElement)elm; Label X = new Label(); X.HorizontalContentAlignment = HorizontalAlignment.Center; X.VerticalContentAlignment = VerticalAlignment.Center; X.Content = l.LabelText; setControlLayout(l, X); } else if (elm is BooleanFormElement) { BooleanFormElement b = (BooleanFormElement)elm; CheckBox X = new CheckBox(); QADataItem qaDataItem = null; if (f.DataItems.Where(x => x.DataDictionaryEntry.FullName == b.DataDictionaryEntry.FullName).Any()) { //qaDataItem = (QADataItem)f.DataItems.Where(x => x.DataDictionaryEntry.FullName == b.DataDictionaryEntry.FullName).First(); } else { //qaDataItem = new QADataItem(); //qaDataItem.EquipmentItem = b.EquipmentItem; } setControlLayout(b, X); } else if (elm is NumericFormElement) { ///create refernce to native type NumericFormElement n = (NumericFormElement)elm; ///Create the control MahApps.Metro.Controls.NumericUpDown t = new MahApps.Metro.Controls.NumericUpDown(); t.Maximum = n.Maximum; t.MinHeight = n.Minimum; t.StringFormat = "{}{0:F" + n.NumberOfDecimals.ToString() + "} " + n.Unit; ///We should never have a NumericFormElement without a DataDictionaryEntry if (n.DataDictionaryEntry != null) { ///This object is the underlying QADataItem that the control will bind to. QADataItem d = null; if (f.DataItems.Where(x => x.DataDictionaryEntry.FullName == n.DataDictionaryEntry.FullName && (x as QADataItem).EquipmentItem.FullName == n.EquipmentItem.FullName).Any()) { ///This FormInstance already has a QADataItem that matches on DataDictionaryEntry and EquipmentItem, so let's retrieve that one. d = f.DataItems.Where(x => x.DataDictionaryEntry.FullName == n.DataDictionaryEntry.FullName && (x as QADataItem).EquipmentItem.FullName == n.EquipmentItem.FullName).First() as QADataItem; } else { d = new QuantifiableQADataItem { EquipmentItem = n.EquipmentItem, DataDictionaryEntry = n.DataDictionaryEntry, QAFormInstance = f as QAFormInstance }; } Binding bd = new Binding("Value"); bd.Source = d; bd.Mode = BindingMode.TwoWay; BindingOperations.SetBinding(t, MahApps.Metro.Controls.NumericUpDown.ValueProperty, bd); } else { // / we should log this } //t.LostFocus += new RoutedEventHandler((senderX, eX) => FormatNumericElement(senderX, eX, n.Format + n.NumberOfDecimals)); setControlLayout(n, t); } else if (elm is TextFormElement) { TextFormElement tfe = (TextFormElement)elm; TextBox t = new TextBox(); setControlLayout(tfe, t); } } }