JayLarsen
JayLarsen

Reputation: 21

Filtering properties displayed in PropertyGrid

Please can someone show me how I can list only properties I want to show in PropertyGrid.

Example make a list or property and show only that property in that list.

Here a nice property grid example, is what I'm using now.

http://hotfile.com/dl/104485386/ce9e469/PropertyGridDemo.rar.html

If can paste example code I appreciate so much.

Upvotes: 2

Views: 3784

Answers (3)

lupok
lupok

Reputation: 1065

There are two paths that can be followed to implement this system:

  • Implement an object wrapper assigned to the PropertyGrid that implements the ICustomTypeDescriptor interface. This solution, besides being complex and having several side effects, has the disadvantage of having to rewrite part of the editors necessary to modify the properties using PropertyGrid .
  • Implement a TypeConverter to apply to the objects managed by the PropertyGrid , the only disadvantage of this solution is the fact of having to apply the TypeConverter to the classes that you want to manage using the PropertyGrid . Apart from this it is the ideal solution and it is the way implemented in this example.

https://www.albertoschiassi.it/Home/tabid/55/EntryId/119/WinForms-Filtered-PropertyGrid.aspx

enter image description here

Upvotes: 3

akjoshi
akjoshi

Reputation: 15772

You can use the Browsable attribute as Anuraj said or in case you want more control (in case you are creating custom controls deriving from other classes/controls and also use your custom property grid control) you can create your own attribute and use that to filter out the properties.

Here is how you can achieve this -

Step 1 - Create your custom attribute

/// <summary>
/// Attribute to identify the Custom Proeprties.
/// Only Proeprties marked with this attribute(true) will be displayed in property grid.
/// </summary>
[global::System.AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class IsCustomPropertyAttribute : Attribute
{
    // See the attribute guidelines at 
    //  http://go.microsoft.com/fwlink/?LinkId=85236

    private bool isCustomProperty;

    public IsCustomPropertyAttribute(bool isCustomProperty)
    {
        this.isCustomProperty = isCustomProperty;
    }

    public bool IsCustomProperty
    {
        get { return isCustomProperty; }
        set { isCustomProperty = value; }
    }

    public override bool IsDefaultAttribute()
    {
        return isCustomProperty == false;
    }
}

Step - 2

In your control(whose properties you want to display) mark every property with this attribute like this -

    [IsCustomProperty(true)]
    [DisplayName("Orientation")]
    public bool ScaleVisibility
    {
        get { return (bool)GetValue(ScaleVisibilityProperty); }
        set { SetValue(ScaleVisibilityProperty, value); }
    }
    public static readonly DependencyProperty ScaleVisibilityProperty =
        DependencyProperty.Register("ScaleVisibility", typeof(bool),
        typeof(IC_BarControl), new UIPropertyMetadata(true));

Step -3

Now, in your property grid code(where you are adding the property in property grid) add a check for this attribute like this -

    //Check if IsCustomPropertyAttribute is defined for this property or not
    bool isCustomAttributeDefined = Attribute.IsDefined(type.GetProperty
          (propertyDescriptor.Name), typeof(IsCustomPropertyAttribute));

    if (isCustomAttributeDefined == true)
       {
          //IsCustomPropertyAttribute is defined so get the attribute
          IsCustomPropertyAttribute myAttribute = 
            Attribute.GetCustomAttribute(type.GetProperty(propertyDescriptor.Name),
            typeof(IsCustomPropertyAttribute)) as IsCustomPropertyAttribute;

           //Check if current property is Custom Property or not
           if (myAttribute != null && myAttribute.IsCustomProperty == true)
               {
                   AddProperty(propertyDescriptor);
               }
        }

Upvotes: 1

Anuraj
Anuraj

Reputation: 19598

If you look at the code, only browsable properties are added.

if (!property.IsBrowsable) continue;

So if you don't want to display a property make it as non-browsable. You can do something like

[Browsable(false)]

If you don't want a property to be displayed on the property grid just provide Browsable attribute and set as false like this.

[Browsable(false)]
public SolidColorBrush Background { get; set; }

Hope it helps

Upvotes: 5

Related Questions