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

            CmsCheckBox checkBox = new CmsCheckBox();
            checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
            checkBox.VerticalAlignment = VerticalAlignment.Center;
            checkBox.Margin = new Thickness(1);

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

            UIElement returnElement = checkBox;
            checkBox.IsEnabled = tuningPropertyWrapViewModel.TuningProperty.IsEditable;

            return returnElement;
        }
        private UIElement BindElementLabel(TuningPropertyWrapViewModel tuningPropertyWrapViewModel, string bindingPath)
        {
            Binding binding = new Binding(bindingPath)
            {
                Mode = BindingMode.TwoWay,
                Source = tuningPropertyWrapViewModel
            };

            Label lastModifiedLabel = new Label();
            lastModifiedLabel.SetBinding(Label.ContentProperty, binding);
            lastModifiedLabel.VerticalAlignment = VerticalAlignment.Center;
            lastModifiedLabel.Margin = new Thickness(1);

            UIElement returnElement = lastModifiedLabel;

            return returnElement;
        }
        private UIElement BindElementValue(TuningPropertyWrapViewModel tuningPropertyWrapViewModel, IEnumerable<PropertyList> propertyLists )
        {
            UIElement returnElement = new CmsCheckBox();

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

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

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

                    if (tuningPropertyWrapViewModel.PropertyType == CommonUtils.PropertyType.LargeText) newCmsTextBox.AcceptsReturn = true;
                    newCmsTextBox.IsReadOnly = !tuningPropertyWrapViewModel.TuningProperty.IsEditable;

                    returnElement = newCmsTextBox;
                    break;

                case CommonUtils.PropertyType.ComboBox:
                        CmsComboBox cmsComboBox = new CmsComboBox();
                        cmsComboBox.Margin = new Thickness(1);

                        var propertyListNames = (from x in propertyLists
                                                 where x.Id == tuningPropertyWrapViewModel.TuningProperty.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
                                             select !string.IsNullOrEmpty(x.Description)
                                             ? x.Name + "  (" + x.Description + ")" :
                                             x.Name).ToList();

                            tuningPropertyWrapViewModel.ListValues = listNames;

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

                            cmsComboBox.ItemTemplate = Utils.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 = tuningPropertyWrapViewModel.TuningProperty.IsEditable;
                        if (!tuningPropertyWrapViewModel.TuningProperty.IsEditable)
                        {
                            cmsComboBox.Foreground = new SolidColorBrush(Color.FromArgb(255, 61, 61, 91));
                        }
                        returnElement = cmsComboBox;

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

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

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

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

                    returnElement = grid;
                    break;
                case CommonUtils.PropertyType.VerifiedCheckBox:

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

                    CmsCheckBox checkBox = new CmsCheckBox();

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

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

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

                    TextBox 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 = tuningPropertyWrapViewModel.PropertyType == CommonUtils.PropertyType.LargeText ? 98 : 23;
            return (FrameworkElement)returnElement;
        }
        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();
        }
        private UIElement BindElementPcsvalue(TuningPropertyWrapViewModel tuningPropertyWrapViewModel, string bindingPath)
        {
            Binding binding = new Binding(bindingPath)
            {
                Mode = BindingMode.OneWay,
                Source = tuningPropertyWrapViewModel
            };

            Binding backgroundColourBinding = new Binding("PcsValueBackgroundColour")
            {
                Mode = BindingMode.TwoWay,
                Source = tuningPropertyWrapViewModel
            };

            TextBox newCmsTextBox = new TextBox();
            newCmsTextBox.SetBinding(TextBox.TextProperty, binding);
            newCmsTextBox.SetBinding(BackgroundProperty, backgroundColourBinding);

            newCmsTextBox.VerticalAlignment = VerticalAlignment.Center;
            newCmsTextBox.Margin = new Thickness(1);
            newCmsTextBox.IsEnabled = tuningPropertyWrapViewModel.TuningProperty.IsEditable;

            UIElement returnElement = newCmsTextBox;

            return returnElement;
        }