Re: Problem with the filtering of a property grid by Marc
Marc
Tue May 06 07:35:43 CDT 2008
And here's an example of both approaches...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class Foo
{
[MyAttribute]
public int BrowseNotSetWithCustom {get;set;}
[Browsable(true)]
public int BrowseTrueNoCustom {get;set;}
[Browsable(false), MyAttribute]
public int BrowseFalseWithCustom {get;set;}
}
class MyAttribute : Attribute {}
static class Program
{
static void Main()
{
// have 3 grids; one default, one using custom
// attributes, one using custom tab
Foo foo = new Foo();
Application.EnableVisualStyles();
PropertyGrid customGrid = new PropertyGrid
{
Left = 400,
Height = 400,
SelectedObject = foo,
BrowsableAttributes = new AttributeCollection()
};
customGrid.PropertyTabs.AddTabType(typeof(MyTab),
PropertyTabScope.Static);
Application.Run(
new Form {
Width = 800,
Height = 450,
Controls = {
new PropertyGrid {
Left = 0,
Height = 400,
SelectedObject = foo
},
new PropertyGrid {
Left = 200,
Height = 400,
BrowsableAttributes = new AttributeCollection(
new MyAttribute()),
SelectedObject = foo
},
customGrid
}
}
);
}
}
public class MyTab : PropertyTab
{
public MyTab() {
}
public override System.Drawing.Bitmap Bitmap
{
get
{
return SystemIcons.Shield.ToBitmap();
}
}
public override bool CanExtend(object extendee)
{
return true;
}
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object component,
Attribute[] attributes)
{
return GetProperties(component, attributes);
}
public override PropertyDescriptorCollection GetProperties(object
component)
{
return GetProperties(component, null);
}
public override PropertyDescriptorCollection GetProperties(object
component, Attribute[] attributes)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(component, attributes);
List<PropertyDescriptor> list = new
List<PropertyDescriptor>(props.Count);
foreach (PropertyDescriptor prop in props)
{
if (prop.Name == "BrowseTrueNoCustom") continue; // skip
list.Add(prop);
}
return new PropertyDescriptorCollection(list.ToArray());
}
public override string TabName
{
get { return "Mwahahah"; }
}
}