G
Guest
Try this code, you can use this to do what you are trying to do
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CustomProps
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.propertyGrid1.SelectedObject=new FilterableDemoClass();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor =
System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 8);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(432, 256);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor =
System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
this.propertyGrid1.PropertyValueChanged += new
System.Windows.Forms.PropertyValueChangedEventHandler(this.propertyGrid1_PropertyValueChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 266);
this.Controls.Add(this.propertyGrid1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.propertyGrid1.SelectedObject=new FilterableDemoClass();
}
private void propertyGrid1_PropertyValueChanged(object s,
System.Windows.Forms.PropertyValueChangedEventArgs e)
{
this.propertyGrid1.Refresh();
}
}
//////////////////////////////////////////////////////////
//Dynamic property filtering.
[
AttributeUsage(AttributeTargets.Property, Inherited=true)
]
class DynamicPropertyFilterAttribute : Attribute
{
string _propertyName;
public string PropertyName
{
get{return _propertyName;}
}
string _showOn;
public string ShowOn
{
get{return _showOn;}
}
public DynamicPropertyFilterAttribute(string propName, string value)
{
_propertyName=propName;
_showOn=value;
}
}
public class FilterablePropertyBase : ICustomTypeDescriptor
{
protected PropertyDescriptorCollection
GetFilteredProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc
=TypeDescriptor.GetProperties(this,attributes,true);
PropertyDescriptorCollection finalProps = new
PropertyDescriptorCollection(new PropertyDescriptor[0]);
foreach(PropertyDescriptor pd in pdc)
{
bool include=false;
bool Dynamic=false;
foreach(Attribute a in pd.Attributes)
{
if(a is DynamicPropertyFilterAttribute)
{
Dynamic=true;
DynamicPropertyFilterAttribute
dpf=(DynamicPropertyFilterAttribute)a;
PropertyDescriptor temp=pdc[dpf.PropertyName];
if(dpf.ShowOn.IndexOf(temp.GetValue(this).ToString())>-1)
{
include=true;
}
}
}
if(!Dynamic || include)
finalProps.Add(pd);
}
return finalProps;
}
#region ICustomTypeDescriptor Members
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this,true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this,attributes,true);
}
EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this,true);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this,true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this,true);
}
public PropertyDescriptorCollection GetProperties(Attribute[]
attributes)
{
return GetFilteredProperties(attributes);
}
PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
return GetFilteredProperties(new Attribute[0]);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this,editorBaseType,true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this,true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this,true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this,true);
}
#endregion
}
class FilterableDemoClass : FilterablePropertyBase
{
public enum FilterSelect
{
One,
Some,
All
}
FilterSelect _chooseOne=FilterSelect.One;
public FilterSelect ChooseOne
{
get{return _chooseOne;}
set{_chooseOne=value;}
}
string _firstProperty;
[
DynamicPropertyFilter("ChooseOne", "One,All")
]
public string FirstProperty
{
get{return _firstProperty;}
set{_firstProperty=value;}
}
string _secondProperty;
[
DynamicPropertyFilter("ChooseOne", "Some,All")
]
public string SecondProperty
{
get{return _secondProperty;}
set{_secondProperty=value;}
}
string _thirdProperty;
[
DynamicPropertyFilter("ChooseOne", "Some,All")
]
public string ThirdProperty
{
get{return _thirdProperty;}
set{_thirdProperty=value;}
}
string _fourthProperty;
[
DynamicPropertyFilter("ChooseOne", "All")
]
public string FourthProperty
{
get{return _fourthProperty;}
set{_fourthProperty=value;}
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CustomProps
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.propertyGrid1.SelectedObject=new FilterableDemoClass();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor =
System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 8);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(432, 256);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor =
System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
this.propertyGrid1.PropertyValueChanged += new
System.Windows.Forms.PropertyValueChangedEventHandler(this.propertyGrid1_PropertyValueChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 266);
this.Controls.Add(this.propertyGrid1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.propertyGrid1.SelectedObject=new FilterableDemoClass();
}
private void propertyGrid1_PropertyValueChanged(object s,
System.Windows.Forms.PropertyValueChangedEventArgs e)
{
this.propertyGrid1.Refresh();
}
}
//////////////////////////////////////////////////////////
//Dynamic property filtering.
[
AttributeUsage(AttributeTargets.Property, Inherited=true)
]
class DynamicPropertyFilterAttribute : Attribute
{
string _propertyName;
public string PropertyName
{
get{return _propertyName;}
}
string _showOn;
public string ShowOn
{
get{return _showOn;}
}
public DynamicPropertyFilterAttribute(string propName, string value)
{
_propertyName=propName;
_showOn=value;
}
}
public class FilterablePropertyBase : ICustomTypeDescriptor
{
protected PropertyDescriptorCollection
GetFilteredProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc
=TypeDescriptor.GetProperties(this,attributes,true);
PropertyDescriptorCollection finalProps = new
PropertyDescriptorCollection(new PropertyDescriptor[0]);
foreach(PropertyDescriptor pd in pdc)
{
bool include=false;
bool Dynamic=false;
foreach(Attribute a in pd.Attributes)
{
if(a is DynamicPropertyFilterAttribute)
{
Dynamic=true;
DynamicPropertyFilterAttribute
dpf=(DynamicPropertyFilterAttribute)a;
PropertyDescriptor temp=pdc[dpf.PropertyName];
if(dpf.ShowOn.IndexOf(temp.GetValue(this).ToString())>-1)
{
include=true;
}
}
}
if(!Dynamic || include)
finalProps.Add(pd);
}
return finalProps;
}
#region ICustomTypeDescriptor Members
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this,true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this,attributes,true);
}
EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this,true);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this,true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this,true);
}
public PropertyDescriptorCollection GetProperties(Attribute[]
attributes)
{
return GetFilteredProperties(attributes);
}
PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
return GetFilteredProperties(new Attribute[0]);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this,editorBaseType,true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this,true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this,true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this,true);
}
#endregion
}
class FilterableDemoClass : FilterablePropertyBase
{
public enum FilterSelect
{
One,
Some,
All
}
FilterSelect _chooseOne=FilterSelect.One;
public FilterSelect ChooseOne
{
get{return _chooseOne;}
set{_chooseOne=value;}
}
string _firstProperty;
[
DynamicPropertyFilter("ChooseOne", "One,All")
]
public string FirstProperty
{
get{return _firstProperty;}
set{_firstProperty=value;}
}
string _secondProperty;
[
DynamicPropertyFilter("ChooseOne", "Some,All")
]
public string SecondProperty
{
get{return _secondProperty;}
set{_secondProperty=value;}
}
string _thirdProperty;
[
DynamicPropertyFilter("ChooseOne", "Some,All")
]
public string ThirdProperty
{
get{return _thirdProperty;}
set{_thirdProperty=value;}
}
string _fourthProperty;
[
DynamicPropertyFilter("ChooseOne", "All")
]
public string FourthProperty
{
get{return _fourthProperty;}
set{_fourthProperty=value;}
}
}
}
ljlevend said:I want to change which properties of a class are visible in a PropertyGrid at
run-time. I know that the System.ComponentModel.Browsable attribute allows
you to specify the visibility of properties in code. But, is there a way to
effectively change the value of the Browsable attribute at run-time? For
example, the ShouldSerialize[PropertyName] and Reset[PropertyName] methods
can be used instead of the System.ComponentModel.DefaultValue attribute in
order to specify the default value at run-time.
Thanks,
Lance