WPF .NET 5 C1PropertyGrid missing layout and sort buttons

Posted by: Jeff.Huckins on 2 February 2022, 10:35 am EST

    • Post Options:
    • Link

    Posted 2 February 2022, 10:35 am EST - Updated 3 October 2022, 11:33 pm EST

    A standard PropertyGrid, for example in .NET framework, includes layout and sorting buttons, etc.

    Why doesn’t the C1PropertyGrid?

  • Posted 2 February 2022, 7:48 pm EST

    Hi Jeff,

    Thanks for the snapshot.

    You can achieve this requirement by adding custom buttons in PropertyGrid’s template to manage the PropertySort property accordingly. For that you need to handle Loaded event of PropertyGrid as:

    
            private void Loaded_PropertyGrid(object sender, RoutedEventArgs e)
            {
                //Adjust Grid for adding Buttons to the top
                var grid = propertyGrid.Template.FindName("3_T",propertyGrid) as Grid;
                grid.RowDefinitions.Insert(0, new RowDefinition() { Height = new GridLength(30) });
                Grid.SetRow((grid.Children[0] as C1ScrollViewer), 1);
                Grid.SetRow((grid.Children[1] as C1Border), 2);
    
                //Add categorized and sort button 
                Button catButton = new Button()
                {
                    Background = Brushes.Transparent,
                    BorderBrush = Brushes.Transparent,
                    Width = 30,
                    Margin = new Thickness(5, 0, 5, 0),
                    BorderThickness = new Thickness(2),
                    Content = new Image() { Source = new BitmapImage(new Uri("../../Icons/list.png", UriKind.Relative)) }
                };
                catButton.Width = 30;
                catButton.Margin = new Thickness(5, 0, 5, 0);
                Button sortButton = new Button() 
                {
                    Background = Brushes.Transparent,
                    BorderBrush = Brushes.Transparent,
                    Width = 30,
                    Margin = new Thickness(5, 0, 5, 0),
                    BorderThickness = new Thickness(2),
                    Content = new Image() { Source = new BitmapImage(new Uri("../../Icons/sort.png", UriKind.Relative)) }
                };
                StackPanel stack = new StackPanel() { Orientation = Orientation.Horizontal};
                stack.Children.Add(catButton);
                stack.Children.Add(sortButton);
                grid.Children.Add(stack);
    
                //Customize buttons
                if (propertyGrid.PropertySort == PropertySort.CategorizedAlphabetical)
                    catButton.BorderBrush = Brushes.Blue;
                else if(propertyGrid.PropertySort == PropertySort.Alphabetical)
                    catButton.BorderBrush = Brushes.Blue;
    
                catButton.Click += (s, e2) =>
                 {
                     propertyGrid.PropertySort = PropertySort.CategorizedAlphabetical;
                     catButton.BorderBrush = Brushes.Blue;
                     sortButton.BorderBrush = Brushes.Transparent;
                 };
                sortButton.Click += (s, e2) =>
                {
                    propertyGrid.PropertySort = PropertySort.Alphabetical;
                    sortButton.BorderBrush = Brushes.Blue;
                    catButton.BorderBrush = Brushes.Transparent;
                };
            }
    
    

    Please refer the attached sample for the same : PropertyGridCustomCategorizeSort.zip

    Best Regards,

    Nitin

  • Posted 3 February 2022, 4:37 am EST

    I’ll give it a try! Thanks, Nitin!

  • Posted 3 February 2022, 4:50 am EST

    Nitin,

    I’m hoping and assuming that C1PropertyGrid works with Property Attributes like:

            [CategoryAttribute("General"),
             DescriptionAttribute("Variable to hold each item in the list for the loop."),
             DefaultValueAttribute("MyVariable")]
            public string Variable
            {
                get { return m_Variable; }
                set 
                {
                    if (m_Variable != value)
                    {
                        m_Variable = value;
                        OnPropertyChanged();
                    }
                }
            }
    
    
  • Posted 3 February 2022, 4:13 pm EST

    Hi Jeff,

    Thanks for the code snippet.

    The above solution will also work for Property Attributes. PropertySort can categorize and sort according to the annotations if applied.

    Just in case if you face any issue then feel free to reach us with your query.

    Best Regards,

    Nitin

  • Posted 4 February 2022, 9:45 am EST

    Looks like I have another question:

    I understand what the “SelectedObject” property is for, but what functionality does the “SelectedObjects” property provide and how would it be used?

  • Posted 6 February 2022, 3:58 pm EST

    Hi Jeff,

    Thanks for reaching out to us with another query.

    As you already understand the SelectedObject property, which is used to get or set the object for which the C1PropertyGrid displays properties. What if, we have to get or set objects for which the C1PropertyGrid displays properties, and these objects may be of different types. In this scenario, we can get or set objects through SelectedObjects.

    Please refer the attached sample to understand how SelectedObjects can be used: PropertyGridSelectedObjectsDemo.zip

    Best Regards,

    Nitin

  • Posted 2 March 2022, 2:13 pm EST

    The Vertical Scroll Bar doesn’t show for the C1PropertyGrid with the added category and sort buttons as you provided.

  • Posted 2 March 2022, 8:33 pm EST - Updated 3 October 2022, 11:33 pm EST

    Hi Jeff,

    We downloaded and run the the attached sample(PropertyGridCustomCategorizeSort.zip) and the vertical scroll bar is visible at our end (see image)

    Please check at your end too. If still scrollbar is not visible then please share a stripped sample for investigation.

    Best Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels