Back to Devexpress

How to: Filter Properties

windowsforms-8369-controls-and-libraries-property-grid-how-to-filter-an-objects-properties-using-the-custompropertydescriptors-event.md

latest5.0 KB
Original Source

How to: Filter Properties

  • Nov 12, 2020
  • 2 minutes to read

This example handles the CustomPropertyDescriptors event to display and hide specific properties in the PropertyGridControl.

View Example

csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraVerticalGrid.Events;

namespace PropertyFiltering {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            this.propertyGridControl1.SelectedObject = this.propertyGridControl1;
            this.propertyGridControl1.GetRowByFieldName("Size").Expanded = true;
            this.propertyGridControl1.GetRowByFieldName("Location").Expanded = true;
        }

        void propertyGridControl1_CustomPropertyDescriptors(object sender, CustomPropertyDescriptorsEventArgs e) {
            // Specify which properties should be displayed at the root level.
            if(e.Context.PropertyDescriptor == null) {
                PropertyDescriptorCollection filteredCollection = new PropertyDescriptorCollection(null);
                AddIfPropertyExist(e.Properties, filteredCollection, "Dock");
                AddIfPropertyExist(e.Properties, filteredCollection, "Size");
                AddIfPropertyExist(e.Properties, filteredCollection, "Location");
                AddIfPropertyExist(e.Properties, filteredCollection, "NonexistentProperty");
                e.Properties = filteredCollection;
            }
            // Specify which nested properties should be displayed for the Size root property.
            if(e.Context.PropertyDescriptor != null && e.Context.PropertyDescriptor.Name == "Size") {
                PropertyDescriptorCollection filteredCollection = new PropertyDescriptorCollection(null);
                AddIfPropertyExist(e.Properties, filteredCollection, "Height");
                e.Properties = filteredCollection;
            }
        }
        void AddIfPropertyExist(PropertyDescriptorCollection sourceCollection, PropertyDescriptorCollection targetCollection, string name) {
            PropertyDescriptor foundPropertyDescriptor = sourceCollection[name];
            if(foundPropertyDescriptor == null)
                return;
            targetCollection.Add(foundPropertyDescriptor);
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraVerticalGrid.Events

Namespace PropertyFiltering
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
            Me.propertyGridControl1.SelectedObject = Me.propertyGridControl1
            Me.propertyGridControl1.GetRowByFieldName("Size").Expanded = True
            Me.propertyGridControl1.GetRowByFieldName("Location").Expanded = True
        End Sub

        Private Sub propertyGridControl1_CustomPropertyDescriptors(ByVal sender As Object, ByVal e As CustomPropertyDescriptorsEventArgs) Handles propertyGridControl1.CustomPropertyDescriptors
            ' Specify which properties should be displayed at the root level.
            If e.Context.PropertyDescriptor Is Nothing Then
                Dim filteredCollection As New PropertyDescriptorCollection(Nothing)
                AddIfPropertyExist(e.Properties, filteredCollection, "Dock")
                AddIfPropertyExist(e.Properties, filteredCollection, "Size")
                AddIfPropertyExist(e.Properties, filteredCollection, "Location")
                AddIfPropertyExist(e.Properties, filteredCollection, "NonexistentProperty")
                e.Properties = filteredCollection
            End If
            ' Specify which nested properties should be displayed for the Size root property.
            If e.Context.PropertyDescriptor IsNot Nothing AndAlso e.Context.PropertyDescriptor.Name = "Size" Then
                Dim filteredCollection As New PropertyDescriptorCollection(Nothing)
                AddIfPropertyExist(e.Properties, filteredCollection, "Height")
                e.Properties = filteredCollection
            End If
        End Sub
        Private Sub AddIfPropertyExist(ByVal sourceCollection As PropertyDescriptorCollection, ByVal targetCollection As PropertyDescriptorCollection, ByVal name As String)
            Dim foundPropertyDescriptor As PropertyDescriptor = sourceCollection(name)
            If foundPropertyDescriptor Is Nothing Then
                Return
            End If
            targetCollection.Add(foundPropertyDescriptor)
        End Sub
    End Class
End Namespace