Пример #1
0
        private void AddPropertyPanel(Panel panel, PropertyItem pi, object instance, Tab tab)
        {
            // TODO: refactor this method - too long and complex...
            var propertyPanel = new Grid();
            if (!pi.FillTab)
            {
                propertyPanel.Margin = new Thickness(2);
            }

            var labelColumn = new System.Windows.Controls.ColumnDefinition
                                  {
                                      Width = GridLength.Auto,
                                      MinWidth = this.MinimumLabelWidth,
                                      MaxWidth = this.MaximumLabelWidth,
                                      SharedSizeGroup =
                                          this.LabelWidthSharing
                                          != LabelWidthSharing.NotShared
                                              ? "labelColumn"
                                              : null
                                  };

            propertyPanel.ColumnDefinitions.Add(labelColumn);
            propertyPanel.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
            var rd = new System.Windows.Controls.RowDefinition
                         {
                             Height =
                                 pi.FillTab
                                     ? new GridLength(1, GridUnitType.Star)
                                     : GridLength.Auto
                         };
            propertyPanel.RowDefinitions.Add(rd);

            var propertyLabel = this.CreateLabel(pi);
            var propertyControl = this.CreatePropertyControl(pi);
            if (propertyControl != null)
            {
                if (!double.IsNaN(pi.Width))
                {
                    propertyControl.Width = pi.Width;
                    propertyControl.HorizontalAlignment = HorizontalAlignment.Left;
                }

                if (!double.IsNaN(pi.Height))
                {
                    propertyControl.Height = pi.Height;
                }

                if (!double.IsNaN(pi.MinimumHeight))
                {
                    propertyControl.MinHeight = pi.MinimumHeight;
                }

                if (!double.IsNaN(pi.MaximumHeight))
                {
                    propertyControl.MaxHeight = pi.MaximumHeight;
                }

                if (pi.IsOptional)
                {
                    propertyControl.SetBinding(
                        IsEnabledProperty,
                        pi.OptionalDescriptor != null ? new Binding(pi.OptionalDescriptor.Name) : new Binding(pi.Descriptor.Name) { Converter = NullToBoolConverter });
                }

                if (pi.IsEnabledByRadioButton)
                {
                    propertyControl.SetBinding(
                        IsEnabledProperty,
                        new Binding(pi.RadioDescriptor.Name) { Converter = new EnumToBooleanConverter() { EnumType = pi.RadioDescriptor.PropertyType }, ConverterParameter = pi.RadioValue });
                }

                var dataErrorInfoInstance = instance as IDataErrorInfo;
                if (dataErrorInfoInstance != null)
                {
                    if (this.ValidationTemplate != null)
                    {
                        Validation.SetErrorTemplate(propertyControl, this.ValidationTemplate);
                    }

                    if (this.ValidationErrorStyle != null)
                    {
                        propertyControl.Style = this.ValidationErrorStyle;
                    }

                    var errorControl = new ContentControl
                                           {
                                               ContentTemplate = this.ValidationErrorTemplate,
                                               Focusable = false
                                           };
                    var errorConverter = new DataErrorInfoConverter(dataErrorInfoInstance, pi.PropertyName);
                    var visibilityBinding = new Binding(pi.PropertyName)
                                      {
                                          Converter = errorConverter,
                                          NotifyOnTargetUpdated = true,
                                          //                                          ValidatesOnDataErrors = false,
#if !NET40
                                          ValidatesOnNotifyDataErrors = false,
#endif
                                          //                                          ValidatesOnExceptions = false
                                      };
                    errorControl.SetBinding(VisibilityProperty, visibilityBinding);

                    // When the visibility of the error control is changed, updated the HasErrors of the tab
                    errorControl.TargetUpdated += (s, e) => tab.UpdateHasErrors(dataErrorInfoInstance);

                    var contentBinding = new Binding(pi.PropertyName)
                                             {
                                                 Converter = errorConverter,
                                                 //                                                 ValidatesOnDataErrors = false,
#if !NET40
                                                 ValidatesOnNotifyDataErrors = false,
#endif
                                                 //                                                 ValidatesOnExceptions = false
                                             };
                    errorControl.SetBinding(ContentControl.ContentProperty, contentBinding);

                    // Add a row to the panel
                    propertyPanel.RowDefinitions.Add(new System.Windows.Controls.RowDefinition { Height = GridLength.Auto });
                    propertyPanel.Children.Add(errorControl);
                    Grid.SetRow(errorControl, 1);
                    Grid.SetColumn(errorControl, 1);
                }

                Grid.SetColumn(propertyControl, 1);
            }

            var actualHeaderPlacement = pi.HeaderPlacement;

            var checkBoxPropertyControl = propertyControl as CheckBox;

            if (checkBoxPropertyControl != null)
            {
                if (this.CheckBoxLayout != CheckBoxLayout.Header)
                {
                    checkBoxPropertyControl.Content = propertyLabel;
                    propertyLabel = null;
                }

                if (this.CheckBoxLayout == CheckBoxLayout.CollapseHeader)
                {
                    actualHeaderPlacement = HeaderPlacement.Collapsed;
                }
            }

            switch (actualHeaderPlacement)
            {
                case HeaderPlacement.Hidden:
                    break;

                case HeaderPlacement.Collapsed:
                    {
                        if (propertyControl != null)
                        {
                            Grid.SetColumn(propertyControl, 0);
                            Grid.SetColumnSpan(propertyControl, 2);
                        }

                        break;
                    }

                default:
                    {
                        // create the label panel
                        var labelPanel = new DockPanel();
                        if (pi.HeaderPlacement == HeaderPlacement.Left)
                        {
                            DockPanel.SetDock(labelPanel, Dock.Left);
                        }
                        else
                        {
                            // Above
                            if (propertyControl != null)
                            {
                                propertyPanel.RowDefinitions.Add(new System.Windows.Controls.RowDefinition());
                                Grid.SetColumnSpan(labelPanel, 2);
                                Grid.SetRow(propertyControl, 1);
                                Grid.SetColumn(propertyControl, 0);
                                Grid.SetColumnSpan(propertyControl, 2);
                            }
                        }

                        propertyPanel.Children.Add(labelPanel);

                        if (propertyLabel != null)
                        {
                            DockPanel.SetDock(propertyLabel, Dock.Left);
                            labelPanel.Children.Add(propertyLabel);
                        }

                        if (this.ShowDescriptionIcons && this.DescriptionIcon != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pi.Description))
                            {
                                var descriptionIconImage = new Image
                                                               {
                                                                   Source = this.DescriptionIcon,
                                                                   Stretch = Stretch.None,
                                                                   Margin = new Thickness(0, 4, 4, 4),
                                                                   VerticalAlignment = VerticalAlignment.Top,
                                                                   HorizontalAlignment =
                                                                       this.DescriptionIconAlignment
                                                               };

                                // RenderOptions.SetBitmapScalingMode(descriptionIconImage, BitmapScalingMode.NearestNeighbor);
                                labelPanel.Children.Add(descriptionIconImage);
                                if (!string.IsNullOrWhiteSpace(pi.Description))
                                {
                                    descriptionIconImage.ToolTip = this.CreateToolTip(pi.Description);
                                }
                            }
                        }
                        else
                        {
                            labelPanel.ToolTip = this.CreateToolTip(pi.Description);
                        }
                    }

                    break;
            }

            // add the property control
            if (propertyControl != null)
            {
                propertyPanel.Children.Add(propertyControl);
            }

            // Set the IsEnabled binding of the label
            if (pi.IsEnabledDescriptor != null && propertyLabel != null)
            {
                var isEnabledBinding = new Binding(pi.IsEnabledDescriptor.Name);
                if (pi.IsEnabledValue != null)
                {
                    isEnabledBinding.ConverterParameter = pi.IsEnabledValue;
                    isEnabledBinding.Converter = ValueToBooleanConverter;
                }

                propertyLabel.SetBinding(IsEnabledProperty, isEnabledBinding);
            }

            // Set the IsEnabled binding of the property control
            if (pi.IsEnabledDescriptor != null && propertyControl != null)
            {
                var isEnabledBinding = new Binding(pi.IsEnabledDescriptor.Name);
                if (pi.IsEnabledValue != null)
                {
                    isEnabledBinding.ConverterParameter = pi.IsEnabledValue;
                    isEnabledBinding.Converter = ValueToBooleanConverter;
                }

                var currentBindingExpression = propertyControl.GetBindingExpression(IsEnabledProperty);
                if (currentBindingExpression != null)
                {
                    var multiBinding = new MultiBinding();
                    multiBinding.Bindings.Add(isEnabledBinding);
                    multiBinding.Bindings.Add(currentBindingExpression.ParentBinding);
                    multiBinding.Converter = AllMultiValueConverter;
                    multiBinding.ConverterParameter = true;
                    propertyControl.SetBinding(IsEnabledProperty, multiBinding);
                }
                else
                {
                    propertyControl.SetBinding(IsEnabledProperty, isEnabledBinding);
                }
            }

            if (pi.IsVisibleDescriptor != null)
            {
                propertyPanel.SetBinding(
                    VisibilityProperty,
                    new Binding(pi.IsVisibleDescriptor.Name) { Converter = BoolToVisibilityConverter });
            }

            if (this.EnableLabelWidthResizing && pi.HeaderPlacement == HeaderPlacement.Left)
            {
                propertyPanel.Children.Add(
                    new GridSplitter
                        {
                            Width = 4,
                            Background = Brushes.Transparent,
                            HorizontalAlignment = HorizontalAlignment.Right,
                            Focusable = false
                        });
            }

            panel.Children.Add(propertyPanel);
        }
Пример #2
0
        /// <summary>
        /// Creates the error control.
        /// </summary>
        public virtual ContentControl CreateErrorControl(PropertyItem pi, object instance, Tab tab, PropertyControlFactoryOptions options)
        {
            var dataErrorInfoInstance       = instance as IDataErrorInfo;
            var notifyDataErrorInfoInstance = instance as INotifyDataErrorInfo;

            var errorControl = new ContentControl
            {
                ContentTemplate = options.ValidationErrorTemplate,
                Focusable       = false
            };
            IValueConverter errorConverter;
            string          propertyPath;
            object          source = null;

            if (dataErrorInfoInstance != null)
            {
                errorConverter = new DataErrorInfoConverter(dataErrorInfoInstance, pi.PropertyName);
                propertyPath   = pi.PropertyName;
                source         = instance;
            }
            else
            {
                errorConverter = new NotifyDataErrorInfoConverter(notifyDataErrorInfoInstance, pi.PropertyName);
                propertyPath   = nameof(tab.HasErrors);
                source         = tab;
                notifyDataErrorInfoInstance.ErrorsChanged += (s, e) =>
                {
                    tab.UpdateHasErrors(notifyDataErrorInfoInstance);
                };
            }

            var visibilityBinding = new Binding(propertyPath)
            {
                Converter             = errorConverter,
                NotifyOnTargetUpdated = true,
#if !NET40
                ValidatesOnNotifyDataErrors = false,
#endif
                Source = source,
            };

            var contentBinding = new Binding(propertyPath)
            {
                Converter = errorConverter,
#if !NET40
                ValidatesOnNotifyDataErrors = false,
#endif
                Source = source,
            };

            errorControl.SetBinding(UIElement.VisibilityProperty, visibilityBinding);

            // When the visibility of the error control is changed, updated the HasErrors of the tab
            errorControl.TargetUpdated += (s, e) =>
            {
                if (dataErrorInfoInstance != null)
                {
                    tab.UpdateHasErrors(dataErrorInfoInstance);
                }
            };
            errorControl.SetBinding(ContentControl.ContentProperty, contentBinding);
            return(errorControl);
        }