private FrameworkElement BindElementValue(ControlTestingPropertyWrapViewModel wrapViewModel, IEnumerable<PropertyList> propertyLists, List<string> pidDocuments, List<string> specDocuments)
        {
            UIElement returnElement = new CmsCheckBox();

            var binding = new Binding("Value")
            {
                Mode = BindingMode.TwoWay,
                Source = wrapViewModel,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            Grid grid;

            switch (wrapViewModel.PropertyType)
            {
                case CommonUtils.PropertyType.Numerical:
                case CommonUtils.PropertyType.Text:
                case CommonUtils.PropertyType.LargeText:
                    var newCmsTextBox = new CmsTextBox();
                    newCmsTextBox.SetBinding(TextBox.TextProperty, binding);
                    newCmsTextBox.VerticalAlignment = VerticalAlignment.Center;
                    newCmsTextBox.Margin = new Thickness(1);

                    newCmsTextBox.ResetOriginalValue();
                    newCmsTextBox.ControlChanged += ControlChanged;

                    if (wrapViewModel.PropertyType == CommonUtils.PropertyType.LargeText) newCmsTextBox.AcceptsReturn = true;

                    returnElement = newCmsTextBox;
                    newCmsTextBox.IsReadOnly = !wrapViewModel.ControlTestingProperty.IsEditable;
                    break;

                //SystemComboBox
                case CommonUtils.PropertyType.SystemComboBox:
                    var cmsSystemComboBox = new CmsComboBox { Margin = new Thickness(1) };

                    var items = new List<string>();

                    if (wrapViewModel.ControlTestingProperty.SystemComboType == CommonUtils.SystemComboBoxType.PandIdDocuments.ToString())
                    {
                        items = pidDocuments;
                    }
                    else if (wrapViewModel.ControlTestingProperty.SystemComboType == CommonUtils.SystemComboBoxType.SpecificationDocuments.ToString())
                    {
                        items = specDocuments;
                    }

                    if (items != null)
                    {
                        wrapViewModel.ListValues = items;

                        var valuesBinding = new Binding("ListValues")
                        {
                            Mode = BindingMode.OneTime,
                            Source = wrapViewModel
                        };

                        cmsSystemComboBox.ItemTemplate = GetPropertyComboDataTemplate();
                        cmsSystemComboBox.SetBinding(ItemsControl.ItemsSourceProperty, valuesBinding);
                        cmsSystemComboBox.SetBinding(Selector.SelectedValueProperty, binding);

                        cmsSystemComboBox.VerticalAlignment = VerticalAlignment.Center;
                        cmsSystemComboBox.Height = 24;

                        cmsSystemComboBox.ResetOriginalValue();
                        cmsSystemComboBox.ControlChanged += ControlChanged;
                    }
                    cmsSystemComboBox.IsEnabled = wrapViewModel.ControlTestingProperty.IsEditable;
                    if (!wrapViewModel.ControlTestingProperty.IsEditable)
                    {
                        cmsSystemComboBox.Foreground = new SolidColorBrush(Color.FromArgb(255, 61, 61, 91));
                    }
                    returnElement = cmsSystemComboBox;
                    break;

                //ComboBox
                case CommonUtils.PropertyType.ComboBox:

                    var cmsComboBox = new CmsComboBox
                    {
                        Margin = new Thickness(1)
                    };

                    var propertyListNames = (from x in propertyLists
                                             where x.Id == wrapViewModel.ControlTestingProperty.PropertyListId
                                             select x.PropertyListNames).FirstOrDefault();

                    if (propertyListNames != null)
                    {
                        //Client asked for a Description to be included in the brackets
                        //In ideal world this should be binded to list of PropertyList objects
                        //and have display item set to a FormattedName
                        //
                        //Current solution is not the great for sure, but best in the current situation
                        //(can't afford to rewrite this)
                        var listNames = (from x in propertyListNames
                                         orderby x.Ordinal
                                         select !string.IsNullOrEmpty(x.Description)
                                             ? x.Name + "  (" + x.Description + ")" :
                                             x.Name).ToList();

                        wrapViewModel.ListValues = listNames;

                        var valuesBinding = new Binding("ListValues")
                        {
                            Mode = BindingMode.OneTime,
                            Source = wrapViewModel
                        };

                        cmsComboBox.ItemTemplate = GetPropertyComboDataTemplate();
                        cmsComboBox.SetBinding(ItemsControl.ItemsSourceProperty, valuesBinding);
                        cmsComboBox.SetBinding(Selector.SelectedValueProperty, binding);

                        cmsComboBox.VerticalAlignment = VerticalAlignment.Center;
                        cmsComboBox.Height = 24;

                        cmsComboBox.ResetOriginalValue();
                        cmsComboBox.ControlChanged += ControlChanged;
                    }
                    cmsComboBox.IsEnabled = wrapViewModel.ControlTestingProperty.IsEditable;
                    if (!wrapViewModel.ControlTestingProperty.IsEditable)
                    {
                        cmsComboBox.Foreground = new SolidColorBrush(Color.FromArgb(255, 61, 61, 91));
                    }
                    returnElement = cmsComboBox;
                    break;

                //CheckBox
                case CommonUtils.PropertyType.CheckBox:
                    var cmsCheckBox = new CmsCheckBox();

                    binding.Converter = new StringToBooleanConverter();
                    cmsCheckBox.SetBinding(ToggleButton.IsCheckedProperty, binding);
                    cmsCheckBox.VerticalAlignment = VerticalAlignment.Center;

                    cmsCheckBox.ResetOriginalValue();
                    cmsCheckBox.ControlChanged += ControlChanged;

                    grid = new Grid();
                    grid.Children.Add(cmsCheckBox);

                    cmsCheckBox.IsEnabled = wrapViewModel.ControlTestingProperty.IsEditable;
                    returnElement = grid;
                    break;

                //VerifiedCheckBox
                case CommonUtils.PropertyType.VerifiedCheckBox:

                    grid = new Grid();
                    grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(20) });
                    grid.ColumnDefinitions.Add(new ColumnDefinition());

                    var checkBox = new CmsCheckBox();

                    binding.Converter = new StringToBooleanConverter();
                    checkBox.SetBinding(ToggleButton.IsCheckedProperty, binding);
                    checkBox.VerticalAlignment = VerticalAlignment.Center;

                    checkBox.ResetOriginalValue();
                    checkBox.ControlChanged += ControlChanged;
                    checkBox.IsEnabled = wrapViewModel.ControlTestingProperty.IsEditable;

                    var verifiedUserDateBinding = new Binding("VerifiedUserDate")
                    {
                        Mode = BindingMode.OneWay,
                        Source = wrapViewModel
                    };

                    var textBox = new TextBox { IsReadOnly = true, VerticalAlignment = VerticalAlignment.Center };
                    textBox.SetBinding(TextBox.TextProperty, verifiedUserDateBinding);

                    grid.Children.Add(checkBox);
                    grid.Children.Add(textBox);

                    Grid.SetRow(checkBox, 0);
                    Grid.SetRow(textBox, 0);
                    Grid.SetColumn(checkBox, 0);
                    Grid.SetColumn(textBox, 1);

                    returnElement = grid;
                    break;
            }

            ((FrameworkElement)returnElement).Height = wrapViewModel.PropertyType == CommonUtils.PropertyType.LargeText ? 98 : 23;
            return (FrameworkElement)returnElement;
        }
        private void BindTextBox(CalibrationComponentPropertyWrapViewModel propertyValue, string bindName, int rowCount, int columnPossition, bool enabled)
        {
            CmsTextBox newCmsTextBox = new CmsTextBox();
            newCmsTextBox.IsEnabled = enabled;

            Binding binding = new Binding(bindName)
            {
                Mode = BindingMode.TwoWay,
                Source = propertyValue,
                ValidatesOnExceptions = true,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };

            newCmsTextBox.SetBinding(TextBox.TextProperty, binding);
            newCmsTextBox.Margin = new Thickness(1);

            newCmsTextBox.ResetOriginalValue();
            newCmsTextBox.ControlChanged += ControlChanged;

            PropertiesGrid.Children.Add(newCmsTextBox);
            Grid.SetRow(newCmsTextBox, rowCount);
            Grid.SetColumn(newCmsTextBox, columnPossition);
        }
        private void PopulatePropertyValues(ControlSystem controlSystem)
        {
            var rowCount = 1;
            const int rowHeight = 25;

            var typeTestingProperties = controlSystem.ControlSystemType.ControlSystemTypeTestingProperties;

            var getAllPropertyListsTask = DatabaseLoader.GetAllPropertyLists();
            var getPidDocumentsTask = DatabaseLoader.GetQuickDocuments(CommonUtils.DoctypePidCode);
            var getSpecDocumentsTask = DatabaseLoader.GetQuickDocuments(CommonUtils.DoctypeFuncspecCode);
            var tasks = new List<Task> { getAllPropertyListsTask, getPidDocumentsTask, getSpecDocumentsTask };

            Task.Factory.ContinueWhenAll(tasks.ToArray(), xx =>
            {
                CMS.UiFactory.StartNew(() =>
                {
                    var addedGroups = new List<ComponentTypeGroup>();
                    var propertyLists = getAllPropertyListsTask.Result;

                    mPidDocuments = getPidDocumentsTask.Result.Select(d => d.Name).ToList();
                    mSpecDocuments = getSpecDocumentsTask.Result.Select(d => d.Name).ToList();

                    List<ControlSystemTypeTestingProperty> list = typeTestingProperties.OrderBy(x => x.Ordinal).ThenBy(x => x.GroupOrdinal).ToList();

                    foreach (var typeTestingProperty in list)
                    {
                        //Skip Properties that are not visible
                        if (typeTestingProperty.TestPropertyId.HasValue && !typeTestingProperty.ControlSystemTestingProperty.IsVisible) continue;

                        #region Group

                        if (typeTestingProperty.ComponentTypeGroupId.HasValue && !addedGroups.Contains(typeTestingProperty.ComponentTypeGroup))
                        {
                            var groupLabel = new Label
                            {
                                Content = typeTestingProperty.ComponentTypeGroup.Name,
                                VerticalAlignment = VerticalAlignment.Center,
                                Margin = new Thickness(1),
                                FontWeight = FontWeights.Bold
                            };

                            PropertiesGrid.Children.Add(groupLabel);
                            Grid.SetRow(groupLabel, rowCount);
                            Grid.SetColumn(groupLabel, (int)GridColumn.Description);
                            PropertiesGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(26) });
                            rowCount++;
                            addedGroups.Add(typeTestingProperty.ComponentTypeGroup);

                            //This is a empty Group so don't need to render a Property
                            if (!typeTestingProperty.TestPropertyId.HasValue) continue;
                        }
                        #endregion

                        #region Property Value

                        var propertyValue = GetPropertyValue(controlSystem, typeTestingProperty);

                        var wrapViewModel = new ControlTestingPropertyWrapViewModel(typeTestingProperty.ControlSystemTestingProperty, propertyValue);
                        PropertyWrapViewModels.Add(wrapViewModel);

                        var element = BindElementValue(wrapViewModel, propertyLists, mPidDocuments, mSpecDocuments);
                        PropertiesGrid.Children.Add(element);
                        Grid.SetRow(element, rowCount);
                        Grid.SetColumn(element, (int)GridColumn.Value);

                        #endregion

                        #region Description

                        var descLabel = new Label();
                        var descLabelBinding = new Binding("ShortDescription")
                        {
                            Mode = BindingMode.OneTime,
                            Source = typeTestingProperty.ControlSystemTestingProperty
                        };

                        descLabel.SetBinding(ContentControl.ContentProperty, descLabelBinding);

                        descLabel.Margin = new Thickness(1);

                        PropertiesGrid.Children.Add(descLabel);
                        Grid.SetRow(descLabel, rowCount);
                        Grid.SetColumn(descLabel, (int)GridColumn.Description);

                        var toolTip = new ToolTip { Content = typeTestingProperty.ControlSystemTestingProperty.Description };
                        ToolTipService.SetToolTip(descLabel, toolTip);

                        #endregion

                        #region Notes

                        //NOTES
                        var binding = new Binding("Notes")
                        {
                            Mode = BindingMode.TwoWay,
                            Source = wrapViewModel,
                            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                        };

                        var notesTextBox = new CmsTextBox();
                        notesTextBox.SetBinding(TextBox.TextProperty, binding);

                        notesTextBox.Margin = new Thickness(1);

                        var toolTipNotes = new ToolTip { Content = wrapViewModel.Notes };
                        ToolTipService.SetToolTip(notesTextBox, toolTipNotes);

                        notesTextBox.ResetOriginalValue();
                        notesTextBox.ControlChanged += ControlChanged;
                        PropertiesGrid.Children.Add(notesTextBox);
                        Grid.SetRow(notesTextBox, rowCount);
                        Grid.SetColumn(notesTextBox, (int)GridColumn.Notes);

                        #endregion

                        if (wrapViewModel.PropertyType == CommonUtils.PropertyType.LargeText)
                        {
                            descLabel.VerticalAlignment = VerticalAlignment.Top;
                            notesTextBox.VerticalAlignment = VerticalAlignment.Stretch;
                        }
                        else
                        {
                            descLabel.VerticalAlignment = VerticalAlignment.Center;
                            notesTextBox.VerticalAlignment = VerticalAlignment.Center;
                        }

                        PropertiesGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(element.Height + 2) });

                        rowCount++;
                    }
                });
            });
        }
        private UIElement BindElementTextBox(TuningPropertyWrapViewModel tuningPropertyWrapViewModel, string bindingPath)
        {
            Binding binding = new Binding(bindingPath)
                {
                    Mode = BindingMode.TwoWay,
                    Source = tuningPropertyWrapViewModel,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                };

            CmsTextBox newCmsTextBox = new CmsTextBox();
            newCmsTextBox.SetBinding(TextBox.TextProperty, binding);
            newCmsTextBox.VerticalAlignment = VerticalAlignment.Center;
            newCmsTextBox.Margin = new Thickness(1);

            newCmsTextBox.ResetOriginalValue();
            newCmsTextBox.ControlChanged += ControlChanged;
            newCmsTextBox.IsEnabled = tuningPropertyWrapViewModel.TuningProperty.IsEditable;

            UIElement returnElement = newCmsTextBox;

            return returnElement;
        }