I'm looking to hide/show properties depending on what selection the user makes in a drop. I am handling the event fine, but cannot actually make the correct properties disappear. The solutions I have found on line are mainly from 2005 and as I've had very little experience with it I thought I must be doing something wrong if hiding properties is this hard.
So far I have tried accessing the property once the event is handled but the ReadOnly and IsBrowsable properties are read only.
The propertygrid has a property BrowsableAttributes that takes an Attribute list but only works negatively and cannot do or - only and. Providing an attribute collection of ; category - 'test' and isbrowsable - true; returns those that match both and as I can't provide multiple AttributeCollections I cannot make the search specific enough to hide the necessary ones whilst leaving others visible.
I have been banging my head against a wall for the past couple of hours thinking there must be an easier way.
Have you tried applying this attribute to a property:
[Browsable(false)]
public object SomeProperty{
}
In that way SomeProperty will not appear on the propertygrid.
To do what you want to do here, you'd need to implement ICustomTypeDescriptor on your class, which is what the PropertyGrid will use to query your class/object to find out what items to display in the PropertyGrid, how to edit them, what category/description they should have, etc.
It can get quite messy, but it seems that there's a pre-written set of helper classes for you on Code Project at http://www.codeproject.com/KB/grid/PropertyGridDynamicProp.aspx.
I'd started writing my own version of the code given at Code Project and after hitting a snag I did some googling and came up with the Code Project link. It looks like they've done a lot better than I was. I've downloaded the code and it seems to work quite well.
If it doesn't solve your problem, please let me know and I'll dig a bit deeper.
Related
I have an existing custom control library with controls which may contain properties: HeaderStyle, ModalStyle, Collapsable, etc...
In the user interface the program is currently displaying a categorized list of these properties. I am trying to update this code to hide properties they dont normally use. I have a list of properties to hide/show based on button click but I am not sure how I can hide these fields programmatically.
I would like to retain any values entered into the fields before hiding and re-display the values if the fields are shown again.
Here is a property that current exists but would like to be hidden/shown on toggle.
/// <summary>ModalStyle property for control</summary>
[XmlAttribute]
[DefaultValue(Utility.Common.Enumerations.ModalStyle.None)]
[Category(PropertyCategories.Rendering)]
[Description("Modal dialog style")]
public ModalStyle? ModalStyle
{
get { return control.ModalStyleActive; }
set { control.ModalStyle = value; }
}
My original though was to do some variant on #if DEBUG but use my own Conditional however I was unable to find a way to change my conditionals via button/toggle.
Can anyone please help with a solution to my problem? I have 20-30 controls with 20 to 30 properties that would like to be filtered.
I have two suggestions that, while they may not give you the exact functionality desired, will keep your solution much more straight forward.
First:
Since you are the library developer you should just decide what properties you want other developers to have access to though the IDE properties window. If a property is seldom used or not very useful through the IDE then just place the [Browsable(false)] attribute on it.
Second:
If you really want all properties to be visible in the IDE properties window, but want to give individuals a way of hiding the more advanced (or less used) ones, just throw them all in an 'Advanced' category. The user can then simply collapse that category and forget about them.
Also: Take a look at Oliver's answer to this question:
[how-to-show-or-hide-properties-dynamically-in-the-propertygrid]
I'm not sure to understand what you are trying to achieve.
When you use Attributes, those are static to the class. So, in your case, when you toggle a show/hide on an object, it's on an instance of the object. Also, you cannot change an attribute value at run-time.
Maybe you should try an alternate solution like creating a global
map<pair<type of object, property name>, is shown>
and update that accordingly from your editor.
And if you want to use something like a property grid, you will have a problem since it won't check your map, but it can be fixed. You could create a new class at run-time and make it a proxy to your current instance. (check on the net how to achieve that, but it's quite simple. There are 2 possibilities: compile from a string or use the ILGenerator.
Hope this help.
I have a custom collection, lets says COL, that derives from ObjectModel.Collection.
I have my own collection editor that works fine when a property, of type COL, is Read and Write enabled.
However, if I change the property to ReadOnly, the open editor button stops showing in the property grid.
As a test, I override my custom editor with the CollectionEditor, and that worked fine.
So, my question is, what check is the property grid making, that CollectionEditor passes but my collection editor fails?
There's not much to override in UITypeEditor, so I fear there's some hard coding going on with regards to CollectionEditor.
Cheers.
ETA:
I've answered the question below.
I've found out why it wasn't appearing and it appears to be a bug in the PropertyGrid.
The button does appear if the EditStyle is set to Modal, but does not appear if it's set to DropDown.
One would have thought that the styles were just for ..., well, style?
Looking in reflector, the issue occurs because additional checks of readonly propertyies (such as checking if it's a reference type - and enabling the button) only happen if the style is set to modal. Hmmm, nice one.
I think the behavior you describe in your own answer is not a bug and is by design, and frankly it's quite logical. Look the dropdown editors around: they offer the user to make a selection and therefore the result is a new value for the property (see ColorEditor, AnchorEditor, DockEditor, CursorEditor, ...). As such the property must not be readonly. A modal editor is more to edit a value (usually a reference type) and therefore can be used even if the property is readonly. Of course, this one can also be used to select a new value (like the FileNameEditor for example).
So maybe the answer is "look at your UI design". Are you sure you should use a dropdown editor to edit the content of a reference type?
I'm looking to develop a custom hierarchy control. I'm having a hard time wrapping my brain around the concept. My question is this. Whether to derive from a Gridview or to derive from a Treeview control. If anyone has any thoughts or links on the overall implementation of either that would be great. Thanks in advance.
My Data is coming back with a level column on each row if this helps.
I've looked at ComponentArt's solution but in many cases right now company funding won't allow for this or the "RadGrid" solutions.
The Treeview seemed like a logical option because it has the relations. I have to make a dynamic one to many relationship. I won't know the parent until I run the query.
Deriving the control would be ideal. Because of the amount of Javascript I've seen in pretty much all examples of this online. I'd like to contain it for other developers to use.
Your question is rather vague. If you want to change the behavior of GridView and you want to create a tabular list like that, you can derive your class from GridView or System.Web.UI.WebControls.DataBoundControl. But TreeView is so different, you can derive your class from System.Web.UI.WebControls.HierarchicalDataBoundControl if you want a hierarchical control or TreeView class for changing behavior.
I'm making a custom control.
Basically what I'm looking for is a OnPreInit event in custom controls. I basically need to know that all of the controls that will be loaded of type MyCustomControl are constructed or not.
Ok, so basically I need to know in the constructor if the current instance being constructed will be the last.
Note: My custom control contains other controls(though this isn't guaranteed) but it will not contain controls of MyCustomControl class.
I've thought about doing an override of AddParsedObject. But if the instance of my custom control doesn't contain any controls, will this still get called?
Unfortunately the approach you're describing is "non-trivial". That means it might not be technically impossible, but so difficult to do correctly and has so many negatives/downsides that you will be better off stepping back and analyzing the larger problem for a different design. Perhaps if you re-posted articulating the end goal you're trying to accomplish, we can help you find an approach that is viable.
I hate to say use a counter, but you're either going to have to pass state information through a dedicated class (or the context,) which you could do in the constructor of the control prior to testing if you've got as many as you need, or you could do a recursive walk through Page.Controls and their .Controls and test each one for type....
I am creating huge amounts of dynamic controls, and for everything that must have an ID assigned, I assign it using a guaranteed unique variable(unless 64bits overflows from just controls).
Now, I have a problem though. I have a duplicate control somewhere and I can not discover where it is added or anything because it doesn't happen until after Page_Load, which means, it is out of my own code when an exception is thrown(I think at like Render or some other internal function).
The control name is "ctlXXX" where XXX is a number(right now, always 244) this control ID is not being made by me. It is being made by ASP.Net automatically(as none of my IDs are prefixed with ctl). So how do I correct this error that I can not see? Can anyone suggest ways of finding my error(I really hope this isn't a bug in ASP.Net)?
Also, I got the error just recently by using a Copy function which will do a Memberwise copy on a custom control, and then it will reset any controls to have either a unique ID or to be null as this part happens before controls are loaded into the custom control, which is a descendant of Panel)
Can anyone give me advice on how to find this bug?
I think you want to look at the INamingContainer interface. I think it will solve your problem but you will have to derive a new control class to do it.