You need to have your classes implement ICustomTypeDesc riptor and return
only those properties with a certain attribute.
The code below my signature shows a simple demo of property filtering
according to the Category attribute. In this case it only shows those
proerties in the appearance category.
Well Formed this month has a more in-depth article on customizing properties
for use in the Property Grid including a dynamic filtering system. Check out
the current issue page
--
Bob Powell [MVP]
C#, System.Drawing
The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
Blog
http://bobpowelldotnet.blogspot.com
------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing. Design;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Reflecti on;
namespace PropertyFilter
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.PropertyG rid propertyGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
Filterable f=new Filterable();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.propertyGr id1 = new System.Windows. Forms.PropertyG rid();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(8, 32);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "With";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(8, 72);
this.button2.Na me = "button2";
this.button2.Ta bIndex = 0;
this.button2.Te xt = "Without";
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// propertyGrid1
//
this.propertyGr id1.Anchor =
((System.Window s.Forms.AnchorS tyles)((((Syste m.Windows.Forms .AnchorStyles.T o
p | System.Windows. Forms.AnchorSty les.Bottom)
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.propertyGr id1.CommandsVis ibleIfAvailable = true;
this.propertyGr id1.LargeButton s = false;
this.propertyGr id1.LineColor = System.Drawing. SystemColors.Sc rollBar;
this.propertyGr id1.Location = new System.Drawing. Point(160, 16);
this.propertyGr id1.Name = "propertyGrid1" ;
this.propertyGr id1.Size = new System.Drawing. Size(272, 240);
this.propertyGr id1.TabIndex = 1;
this.propertyGr id1.Text = "propertyGrid1" ;
this.propertyGr id1.ViewBackCol or = System.Drawing. SystemColors.Wi ndow;
this.propertyGr id1.ViewForeCol or = System.Drawing. SystemColors.Wi ndowText;
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(440, 266);
this.Controls.A dd(this.propert yGrid1);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.button2 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
this.f.Filtered =true;
this.propertyGr id1.SelectedObj ect=f;
this.propertyGr id1.Refresh();
}
private void button2_Click(o bject sender, System.EventArg s e)
{
this.f.Filtered =false;
this.propertyGr id1.SelectedObj ect=f;
this.propertyGr id1.Refresh();
}
}
class Filterable : ICustomTypeDesc riptor
{
bool _filtered;
public bool Filtered
{
get{return _filtered;}
set{_filtered=v alue;}
}
string _stringOne;
[Category("Other ")]
public string StringOne
{
get{return _stringOne;}
set{_stringOne= value;}
}
string _stringTwo;
[Category("Appea rance")]
public string StringTwo
{
get{return _stringTwo;}
set{_stringTwo= value;}
}
#region ICustomTypeDesc riptor Members
public TypeConverter GetConverter()
{
// TODO: Add Filterable.GetC onverter implementation
return TypeDescriptor. GetConverter(th is,true);
}
public EventDescriptor Collection GetEvents(Attri bute[] attributes)
{
// TODO: Add Filterable.GetE vents implementation
return TypeDescriptor. GetEvents(this, attributes,true );
}
EventDescriptor Collection
System.Componen tModel.ICustomT ypeDescriptor.G etEvents()
{
// TODO: Add
Filterable.Syst em.ComponentMod el.ICustomTypeD escriptor.GetEv ents
implementation
return TypeDescriptor. GetEvents(this, true);
}
public string GetComponentNam e()
{
// TODO: Add Filterable.GetC omponentName implementation
return TypeDescriptor. GetComponentNam e(this,true);
}
public object GetPropertyOwne r(PropertyDescr iptor pd)
{
// TODO: Add Filterable.GetP ropertyOwner implementation
return this;
}
public AttributeCollec tion GetAttributes()
{
// TODO: Add Filterable.GetA ttributes implementation
return TypeDescriptor. GetAttributes(t his,true);
}
public PropertyDescrip torCollection GetProperties(A ttribute[] attributes)
{
// TODO: Add Filterable.GetP roperties implementation
PropertyDescrip torCollection
pdc=TypeDescrip tor.GetProperti es(this,attribu tes,true);
if(_filtered)
{
PropertyDescrip torCollection pds=new PropertyDescrip torCollection(n ew
PropertyDescrip tor[0]);
foreach(Propert yDescriptor pd in pdc)
{
Attribute a=pd.Attributes[typeof(Category Attribute)];
if(a!=null && ((CategoryAttri bute)a).Categor y=="Appearance" )
{
pds.Add(pd);
}
}
return pds;
}
else
return TypeDescriptor. GetProperties(t his,attributes, true);
}
PropertyDescrip torCollection
System.Componen tModel.ICustomT ypeDescriptor.G etProperties()
{
// TODO: Add
Filterable.Syst em.ComponentMod el.ICustomTypeD escriptor.GetPr operties
implementation
PropertyDescrip torCollection pdc=TypeDescrip tor.GetProperti es(this,true);
if(_filtered)
{
PropertyDescrip torCollection pds=new PropertyDescrip torCollection(n ew
PropertyDescrip tor[0]);
foreach(Propert yDescriptor pd in pdc)
{
Attribute a=pd.Attributes[typeof(Category Attribute)];
if(a!=null && ((CategoryAttri bute)a).Categor y=="Appearance" )
{
pds.Add(pd);
}
}
return pds;
}
else
return pdc;
}
public object GetEditor(Type editorBaseType)
{
// TODO: Add Filterable.GetE ditor implementation
return TypeDescriptor. GetEditor(this, typeof(UITypeEd itor), true);
}
public PropertyDescrip tor GetDefaultPrope rty()
{
// TODO: Add Filterable.GetD efaultProperty implementation
return TypeDescriptor. GetDefaultPrope rty(this, true);
}
public EventDescriptor GetDefaultEvent ()
{
// TODO: Add Filterable.GetD efaultEvent implementation
return TypeDescriptor. GetDefaultEvent (this,true);
}
public string GetClassName()
{
// TODO: Add Filterable.GetC lassName implementation
return TypeDescriptor. GetClassName(th is,true);
}
#endregion
}
}
------------------------------------------------------------------
"Mevar81" <to*****@hotmai l.com> wrote in message
news:07******** *************** *****@phx.gbl.. .
Hi to everybody.I have a problem with the PropertyGrid
control.I want to display not all the properties of a
generic Control(Button, TextBox,ComboBo x,ecc.).In general
I don't want to display only one category(Appear ance,
Behavior,ecc.) but I want to chose directly which
properties to show.I've read that I can use the
SelectedObjects to put an array of object with some
properties in common with the SelectedObject, and only
properties in common for all the object will be
displayed.I've tryed to build an object with only some
properties and I've tryed to use like I've explain but it
doesn't work(it displays all the properties of the
control).Anyone has some idea to solve my problem?Thank's
to everybody
.