I am using a PropertyGrid for configuring objects. I need to be able to hide or show some properties depending on the values of other properties. How this can be achieved? I know about Browsable attribute, but it only works at a compile time.
Take a look into the ICustomTypeDescriptor Interface.
Further informations on how to use it can be found in this article:
Bending the .NET PropertyGrid to Your Will.
Check this link Changing Browsable Property Attribute dynamically.A sample method is given.
Using Reflection access the Property and set its browsable property to true or false.
Related
I have a working PropertyGrid which in some cases goes disabled.
now I have a new case, in which I need it to enable only one property, is there an easy way of doing it?
thanks
Instead of disabling PropertyGrid, you could mark all relevant nested properties as readonly in your data class, in order to show them disabled in the property grid.
If you want to add ReadOnly attributes at runtime have a look at: https://www.csharp-examples.net/readonly-propertygrid/
Otherwise I'd use a DataGrid or something similar.
What is the name of this form?(That is if I want to search for it, what should I search for?) and is there a way to extend it ?
The control on the right side is a PropertyGrid - so i guess it is filled by reflection. If you derive a custom type from DataColumn, this control should show all new properties as well. Here is a detailed description, which attributes you should use to decorate your properties, so that a description shows up, the right editor is shown, etc.
I have a PropertyGrid with a lot of nested items. How can I make a searchbox to filter and display only the items matching the search string?
Two ways -
Your best option is to implement ICustomTypeDescriptor interface and use the GetProperties() method to filter visible rows in PropertyGrid.
If you want to hack your way around - Using reflection set Browsable Attribute to false. Sample code
Here are some sample articles/links with code to implement ICustomTypeDescriptor -
http://wraithnath.blogspot.in/2011/01/implementing-icustomtypedescriptor-for.html
PropertyGrid Browsable not found for entity framework created property, how to find it?
http://www.codeproject.com/Articles/189521/Dynamic-Properties-for-PropertyGrid
I have around 30 elements/objects for which i need PropertyGrid to show their properties in it,but the problem is that every object has different properties so i created a database for it.
I don't know how to add properties in PropertyGrid from the Database.
I am going to assume that you are using Windows Forms, since you are asking about PropertyGrid. If you have objects (meaning classes) that have the properties you want to display in your PropertyGrid, you need only to set PropertyGrid.SelectedObject with the object you want to display. By default, PropertyGrid will use reflection to find all the public properties of your object, and will display them.
You can use various attributes to control how PropertyGrid displays properties. For example, you can apply the Description attribute to a class property to add help text that the property grid will display. You can use the Browsable attribute to control whether PropertyGrid will display a given property. There are other attributes in the System.ComponentModel namespace that you can use.
I need to change the editor for a property attached to a .net PropertyGrid, but I cannot set the Editor attribute of that property, because the property was generated by a tool, including all attributes.
The desired editor is
System.Windows.Forms.Design.FileNameEditor
I can find many tutorials on the web to assign this editor to string properties by setting the editor attribute, but I cannot set the editor attribute in this case. However, I can advise the code generation tool to behave such that the string property which I want to be edited with the FileNameEditor becomes converted to another property with different type.
In order to do so, I would need to know which type is edited by the FileNameEditor as default. I didn't find any list on the web which would tell me for a given editor which types employ this editor automatically when displayed in a PropertyGrid.
This editor works with strings only and is not attached automatically to any other type.
According to Reflector, it's used only for 2 properties :
SqlConnectionStringBuilder.AttachDBFileName
OleDbConnectionStringBuilder.FileName