private void BindLabel(CalibrationComponentTypeProperty calibrationComponentProperty, string path,int rowCount, int column, bool bindTooltip)
        {
            Label label = new Label();

            Binding labelBinding = new Binding(path)
            {
                Mode = BindingMode.OneTime,
                Source = calibrationComponentProperty
            };

            label.SetBinding(ContentControl.ContentProperty, labelBinding);
            label.Margin = new Thickness(1);

            if (bindTooltip)
            {
                Silverlight.Controls.ToolTips.ToolTip nameTip = new Silverlight.Controls.ToolTips.ToolTip
                {
                    Content = calibrationComponentProperty.CalibrationProperty.Name
                };
                ToolTipService.SetToolTip(label, nameTip);
            }

            PropertiesGrid.Children.Add(label);
            Grid.SetRow(label, rowCount);
            Grid.SetColumn(label, column);
        }
        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 void PopulatePropertyValues(ControlSystemComponent controlSystemComponent)
        {
            int rowCount = 1;
            const int rowHeight = 25;

            //var controlSystemComponentProperies = controlSystemComponent.ControlSystemComponentType.ControlSystemComponentTypeProperties.OrderBy(x => x.Ordinal).ToList();
            var controlSystemComponentTypeTuningProperties =
                controlSystemComponent.ControlSystemComponentType.ControlSystemComponentTypeTuningProperties.OrderBy(x => x.Ordinal).ToList();

            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
            cmsWebServiceClient.GetAllPropertyListsCompleted += (s, e) =>
            {
                List<ComponentTypeGroup> addedGroups = new List<ComponentTypeGroup>();
                //controlSystemComponentProperies.Where(x => x.ComponentTypeGroupId.HasValue).ToList().ForEach(x => x.Ordinal = x.ComponentTypeGroup.Ordinal);
                controlSystemComponentTypeTuningProperties = controlSystemComponentTypeTuningProperties.
                    OrderBy(x => x.Ordinal).ThenBy(x => x.GroupOrdinal).ToList();

                foreach (var controlSystemComponentTypeTuningProperty in controlSystemComponentTypeTuningProperties.Where(x => x.ControlSystemTuningProperty.IsVisible))
                {
                    if (controlSystemComponentTypeTuningProperty.ComponentTypeGroupId.HasValue && !addedGroups.Contains(controlSystemComponentTypeTuningProperty.ComponentTypeGroup))
                    {
                        Label groupLabel = new Label
                        {
                            Content = controlSystemComponentTypeTuningProperty.ComponentTypeGroup.Name,
                            VerticalAlignment = VerticalAlignment.Center,
                            Margin = new Thickness(1),
                            FontWeight = FontWeights.Bold
                        };

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

                    //Property Label
                    Label label = new Label();

                    Binding labelBinding = new Binding("Name")
                    {
                        Mode = BindingMode.OneTime,
                        Source = controlSystemComponentTypeTuningProperty.ControlSystemTuningProperty
                    };

                    label.SetBinding(ContentControl.ContentProperty, labelBinding);
                    label.VerticalAlignment = VerticalAlignment.Center;
                    label.Margin = new Thickness(1);

                    Silverlight.Controls.ToolTips.ToolTip nameTip = new Silverlight.Controls.ToolTips.ToolTip
                    {
                        Content = controlSystemComponentTypeTuningProperty.ControlSystemTuningProperty.Description
                    };
                    ToolTipService.SetToolTip(label, nameTip);

                    PropertiesGrid.Children.Add(label);
                    Grid.SetRow(label, rowCount);
                    Grid.SetColumn(label, 0);

                    var propertyValue = GetPropertyValue(controlSystemComponent, controlSystemComponentTypeTuningProperty);

                    var tuningPropertyWrapViewModel = new TuningPropertyWrapViewModel(controlSystemComponentTypeTuningProperty.ControlSystemTuningProperty, propertyValue);
                    TuningPropertyWrapViewModels.Add(tuningPropertyWrapViewModel);

                    //Property VALUE
                    var element = BindElementValue(tuningPropertyWrapViewModel, e.Result);
                    PropertiesGrid.Children.Add(element);
                    Grid.SetRow((FrameworkElement) element, rowCount);
                    Grid.SetColumn((FrameworkElement) element, 1);

                    //PCS Value
                    var pcsValueElement = BindElementPcsvalue(tuningPropertyWrapViewModel, "PcsValue");
                    pcsValueElement.IsHitTestVisible = false;
                    //((CmsTextBox)pcsValueElement).Background = new SolidColorBrush(Color.FromArgb(255, 239, 239, 239));

                    PropertiesGrid.Children.Add(pcsValueElement);
                    Grid.SetRow((FrameworkElement) pcsValueElement, rowCount);
                    Grid.SetColumn((FrameworkElement) pcsValueElement, 2);

                    //Trended
                    var trendedElement = BindCheckBoxElement(tuningPropertyWrapViewModel, "Trended");
                    ((FrameworkElement) trendedElement).HorizontalAlignment = HorizontalAlignment.Center;

                    PropertiesGrid.Children.Add(trendedElement);
                    Grid.SetRow((FrameworkElement) trendedElement, rowCount);
                    Grid.SetColumn((FrameworkElement) trendedElement, 3);

                    //Notes
                    var notesElement = BindElementTextBox(tuningPropertyWrapViewModel, "Notes");

                    PropertiesGrid.Children.Add(notesElement);
                    Grid.SetRow((FrameworkElement) notesElement, rowCount);
                    Grid.SetColumn((FrameworkElement) notesElement, 4);

                    //Accept
                    Button acceptButton = new Button {Content = "Accept"};
                    acceptButton.Height = 23;
                    acceptButton.DataContext = tuningPropertyWrapViewModel;
                    acceptButton.Command = tuningPropertyWrapViewModel.AcceptCommand;
                    acceptButton.IsEnabled = controlSystemComponentTypeTuningProperty.ControlSystemTuningProperty.IsEditable;

                    Binding visibilityBinding = new Binding("AcceptButtonVisibility")
                    {
                        Mode = BindingMode.OneWay,
                        Source = tuningPropertyWrapViewModel
                    };
                    acceptButton.SetBinding(VisibilityProperty, visibilityBinding);

                    acceptButton.Width = 60;
                    PropertiesGrid.Children.Add(acceptButton);
                    Grid.SetRow((FrameworkElement) acceptButton, rowCount);
                    Grid.SetColumn((FrameworkElement) acceptButton, 5);

                    //ModfiedUserDate
                    var modfiedUserDate = BindElementLabel(tuningPropertyWrapViewModel, "ModfiedUserDate");
                    PropertiesGrid.Children.Add(modfiedUserDate);
                    Grid.SetRow((FrameworkElement) modfiedUserDate, rowCount);
                    Grid.SetColumn((FrameworkElement) modfiedUserDate, 6);

                    PropertiesGrid.RowDefinitions.Add(new RowDefinition {Height = new GridLength(rowHeight)});

                    rowCount++;
                }
            };
            cmsWebServiceClient.GetAllPropertyListsAsync();
        }