private static object CoerceSelectedObjects(DependencyObject d, object value)
        {
            WpfPropertyGrid pg = d as WpfPropertyGrid;

            object single = pg.GetValue(SelectedObjectsProperty);

            return(single == null ? new object[0] : value);
        }
        private static void PropertySortPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            WpfPropertyGrid pg   = source as WpfPropertyGrid;
            PropertySort    sort = (PropertySort)e.NewValue;

            bool isAlpha = (sort == PropertySort.Alphabetical || sort == PropertySort.NoSort);

            pg.IsInAlphaViewMethod.Invoke(pg.Designer.PropertyInspectorView, new object[] { isAlpha });
        }
        private static void SelectedObjectsPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            WpfPropertyGrid pg = source as WpfPropertyGrid;

            pg.CoerceValue(SelectedObjectsProperty);

            object[] collection = e.NewValue as object[];

            if (collection.Length == 0)
            {
                pg.OnSelectionChangedMethod.Invoke(pg.Designer.PropertyInspectorView, new object[] { null });
            }
            else
            {
                bool same  = true;
                Type first = null;

                var       context   = new EditingContext();
                var       mtm       = new ModelTreeManager(context);
                Selection selection = null;

                // Accumulates the selection and determines the type to be shown in the top of the PG
                for (int i = 0; i < collection.Length; i++)
                {
                    mtm.Load(collection[i]);
                    if (i == 0)
                    {
                        selection = Selection.Select(context, mtm.Root);
                        first     = collection[0].GetType();
                    }
                    else
                    {
                        selection = Selection.Union(context, mtm.Root);
                        if (!collection[i].GetType().Equals(first))
                        {
                            same = false;
                        }
                    }
                }

                pg.OnSelectionChangedMethod.Invoke(pg.Designer.PropertyInspectorView, new object[] { selection });
            }

            pg.ChangeHelpText(string.Empty);
        }
        private static void ConStrVisiblePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            WpfPropertyGrid pg = source as WpfPropertyGrid;

            if (e.NewValue != e.OldValue)
            {
                if (e.NewValue.Equals(true))
                {
                    pg.RowDefinitions[1].Height = new GridLength(5);
                    pg.RowDefinitions[2].Height = new GridLength(pg.TextBoxTextHeight);
                }
                else
                {
                    pg.TextBoxTextHeight        = pg.RowDefinitions[2].Height.Value;
                    pg.RowDefinitions[1].Height = new GridLength(0);
                    pg.RowDefinitions[2].Height = new GridLength(0);
                }
            }
        }
        private static void SelectedObjectPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            WpfPropertyGrid pg = source as WpfPropertyGrid;

            pg.CoerceValue(SelectedObjectsProperty);

            if (e.NewValue == null)
            {
                pg.OnSelectionChangedMethod.Invoke(pg.Designer.PropertyInspectorView, new object[] { null });
            }
            else
            {
                var context = new EditingContext();
                var mtm     = new ModelTreeManager(context);
                mtm.Load(e.NewValue);
                Selection selection = Selection.Select(context, mtm.Root);

                pg.OnSelectionChangedMethod.Invoke(pg.Designer.PropertyInspectorView, new object[] { selection });
                ((AdoDotNetConnectionProperties)pg.SelectedObject).PropertyValueChanged += WpfPropertyGrid_PropertyValueChanged;
                ((AdoDotNetConnectionProperties)pg.SelectedObject).ErrorValidating      += WpfPropertyGrid_ErrorValidating;
            }

            pg.ChangeHelpText(((AdoDotNetConnectionProperties)e.NewValue).ToDisplayString());
        }
        private static void ToolbarVisiblePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            WpfPropertyGrid pg = source as WpfPropertyGrid;

            pg.PropertyToolBar.Visibility = e.NewValue.Equals(true) ? Visibility.Visible : Visibility.Collapsed;
        }
        /// <summary>Default constructor, creates the UIElements including a PropertyInspector</summary>
        public WpfPropertyGrid()
        {
            this.ColumnDefinitions.Add(new ColumnDefinition());
            this.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(1, GridUnitType.Star)
            });
            this.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(0)
            });
            this.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(0)
            });
            this.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(0)
            });

            this.Designer = new WorkflowDesigner();

            TextBlock error = new TextBlock()
            {
                Visibility   = Visibility.Visible,
                TextWrapping = TextWrapping.Wrap,
                TextTrimming = TextTrimming.CharacterEllipsis,
                Background   = (SolidColorBrush) new BrushConverter().ConvertFrom("#FFFFECED"),
                Foreground   = (SolidColorBrush) new BrushConverter().ConvertFrom("#FFBC316A")
            };

            TextBlock descrip = new TextBlock()
            {
                Visibility   = Visibility.Visible,
                TextWrapping = TextWrapping.Wrap,
                TextTrimming = TextTrimming.CharacterEllipsis
            };
            DockPanel dock = new DockPanel()
            {
                Visibility    = Visibility.Visible,
                LastChildFill = true,
                Margin        = new Thickness(3, 0, 3, 0)
            };

            descrip.SetValue(DockPanel.DockProperty, Dock.Top);

            dock.Children.Add(descrip);
            this.ErrorText = new Border()
            {
                Visibility  = Visibility.Visible,
                BorderBrush = SystemColors.ActiveBorderBrush,
                //Background = SystemColors.ControlBrush,
                BorderThickness = new Thickness(1),
                Child           = error
            };
            this.ConStrText = new Border()
            {
                Visibility  = Visibility.Visible,
                BorderBrush = SystemColors.ActiveBorderBrush,
                //Background = SystemColors.ControlBrush,
                BorderThickness = new Thickness(1),
                Child           = dock
            };
            this.Splitter = new GridSplitter()
            {
                Visibility          = Visibility.Visible,
                ResizeDirection     = GridResizeDirection.Rows,
                Height              = 5,
                HorizontalAlignment = HorizontalAlignment.Stretch
            };

            var inspector = Designer.PropertyInspectorView;

            inspector.Visibility = Visibility.Visible;
            inspector.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Stretch);


            this.Splitter.SetValue(Grid.RowProperty, 1);
            this.Splitter.SetValue(Grid.ColumnProperty, 0);

            this.ConStrText.SetValue(Grid.RowProperty, 2);
            this.ConStrText.SetValue(Grid.ColumnProperty, 0);

            this.ErrorText.SetValue(Grid.RowProperty, 3);
            this.ErrorText.SetValue(Grid.ColumnProperty, 0);

            Binding binding = new Binding("Parent.Background");

            descrip.SetBinding(BackgroundProperty, binding);

            this.Children.Add(inspector);
            this.Children.Add(this.Splitter);
            this.Children.Add(this.ConStrText);
            this.Children.Add(this.ErrorText);

            Type inspectorType = inspector.GetType();
            var  props         = inspectorType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
                                                             BindingFlags.DeclaredOnly);

            var methods = inspectorType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
                                                   BindingFlags.DeclaredOnly);

            this.RefreshMethod = inspectorType.GetMethod("RefreshPropertyList",
                                                         BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            this.IsInAlphaViewMethod = inspectorType.GetMethod("set_IsInAlphaView",
                                                               BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            this.OnSelectionChangedMethod = inspectorType.GetMethod("OnSelectionChanged",
                                                                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            this.PropertyToolBar = inspectorType.GetMethod("get_PropertyToolBar",
                                                           BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
                                                           BindingFlags.DeclaredOnly).Invoke(inspector, new object[0]) as Control;

            //inspectorType.GetEvent("GotFocus").AddEventHandler(this,
            //    Delegate.CreateDelegate(typeof(RoutedEventHandler), this, "GotFocusHandler", false));
            //inspectorType.GetEvent("LostFocus").AddEventHandler(this,
            //    Delegate.CreateDelegate(typeof(RoutedEventHandler), this, "LostFocusHandler", false));
            current = this;
        }