|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe wonderful But sometimes, you only want to expose just some properties, not all of them. Suppose you want to allow the user of your application to edit the appearance of your main form. How would you do that ? In fact, this can be done very easily. Microsoft thought about this matter and gave us a very interesting property, named BrowsableAttributes: the easy but insufficient wayWith the // You first create the attribute
// you want to filter with :
Attribute myfilterattribute =
new CategoryAttribute("Appearance");
// And you pass it to the PropertyGrid,
// via its BrowsableAttributes property :
mypropertygrid.BrowsableAttributes = new
AttributeCollection(new Attribute[]
{ myfilterattribute });
You can choose several attributes, and of any sort: mypropertygrid.BrowsableAttributes =
new AttributeCollection(new Attribute[] {
new CategoryAttribute("Appearance"),
new CategoryAttribute("Layout"),
new DisplayNameAttribute("zzzz"),
new BrowsableAttribute(false)
});
Note that by default, all the browsable attributes are displayed. In other words, if you let the This is simple and works fine. But how can we just filter by properties? Unfortunately, the Inheritance for selected properties: the FilteredPropertyGridNow, we'll discuss about another problem. Suppose your I propose another approach: using a wrapper object that contains the object you want to display in the
Here's how you can use it. To display only the " // Create the TextBox.
TextBox mycontrol = new TextBox();
// Create the inherited PropertyGrid.
FilteredPropertyGrid mygrid = new FilteredPropertyGrid();
// Set the only property visible.
mygrid.BrowsableProperties = new string[] { "Size" };
// Attach the control to the PropertyGrid.
mygrid.SelectedObject = mycontrol;
// Force the PropertyGrid to redraw itself
mygrid.Refresh();
That's all! You can combine these four properties as you wish. Suppose you want to display all the categories, except the "Accessibility" and "Layout" ones. But you want the " mygrid.HiddenPAttributes =
new AttibuteCollection(new Attribute[] {
new CategoryAttribute("Accessibility"),
new CategoryAttribute("Layout") });
mygrid.HiddenProperties = new string[] { "DataBinidngs" };
mygrid.BrowsableProperties =
new string[] { "AccessibilityRole" }
Note: these properties only work with How does it work?Well, I will just describe the big pants of the public class FilteredPropertyGrid : PropertyGrid
{
private List<PropertyDescriptor>
m_PropertyDescriptors =
new List<PropertyDescriptor>();
private ObjectWrapper m_Wrapper = null;
( ... )
public new object SelectedObject {
get { return m_Wrapper != null ?
((ObjectWrapper)
base.SelectedObject).SelectedObject :
null; }
set {
if(m_Wrapper == null) {
m_Wrapper = new ObjectWrapper(value);
m_Wrapper.PropertyDescriptors =
m_PropertyDescriptors;
RefreshProperties();
} else {
// Link the wrapper to the parent PropertyGrid.
base.SelectedObject = m_Wrapper;
}
}
}
( ... )
}
The
|
||||||||||||||||||||||