Italic text in a PropertyGrid - c#

I'm using a PropertyGrid to show custom properties that are exposed through the implementation of ICustomTypeDescriptor.
My objects are setup in a tree structure and values for each property are either set in each object or inherited from parent objects. In the PropertyGrid I want to visually show the user what properties values are set in the selected object, and which are inherited from parent objects.
Right now I am showing every property it two categories. One set shows what the value is set to in the actual object, with a blank field if it not set. The other set shows the property values assigned to the object that are either set in the object, or inherited if not set in the object.
I would like to combine these two groups into one buy showing set properties in regular text, and inherited values in italic text. However, there doesn't seem to be any way to do that through ICustomTypeDescriptor.GetProperties(). And I don't have easy access to the properties of the PropertyGrid since they get created while the program is running.

You can't do italics - but you can do bold; the bold behaviour is determined by the PropertyDescriptor's ShouldSerializeValue; you can wrap PropertyDescriptors via various System.ComponentModel tricks (ICustomTypeDescriptor, TypeConverter or TypeDescriptionProvider) and provide your own PropertyDescriptor.
Alternatively, there are similar grids with more options, such as by VisualHint - see "Property customization" on that page.

Related

Superclass/Interface providing TextAlignment and FontFamily properties

I am trying to set properties of several different WPF RichTextBox controls, like List, ListItem, Paragraph. The properties I want to set are, for example, FontFamily and TextAlignment.
I want to do this as generic as possible, so I have one setter for TextAlignment, one setter for FontFamily, and so on, but I cannot find out the common superclass providing all these properties.
Can you tell me what superclass I am looking for, and, if possible, how to find out what superclass provides different properties in general?
Edit: More detailed explanation of the case:
FontFamily is inherited from TextElement, in both Paragraph and ListItem
TextAdjust is inherited from Block in apparently every class but ListItem
You can override metadata for a given UIElement.
For example, if you want to set the default FontSize of all FrameworkElements:
System.Windows.Documents
.TextElement
.FontSizeProperty
.OverrideMetadata(typeof(FrameworkElement), new PropertyMetadata(5));
UIElement/FrameworkElement is as generic as it gets, if you want to apply those defaults to only a few types you need to repeat that line for every type you want.
The common base class that holds all the Font... properties is TextElement, whereas TextAlignment is a property of ListItem.
You can easily find this out by yourself when you browse the MSDN pages. There is a tree view on the left that shows Constructors, Fields, Methods, Properties and Events of a class. If you click one of these items it shows a list of items that are defined in that class.
All the properties you are trying to set are dependency properties. You can use the DependencyObject.SetValue method, e.g.:
DependencyObject x;
x.SetValue(Block.TextAlignmentProperty, TextAlignment.Justify);
It's just a matter of finding the correct dependency property descriptions then.

C# propertyGrid miscellaneous questions about properties

I have several questions about properties displayed in propertyGrid i cant seem to find answers for:
1) What do i do if my property is a class? When multiple objects are edited, straaange things happen (when property is struct or simple type, its okay, but if property is class, it passes same class's reference to all edited members when value is applied, which is obviously awful, and it wont recognise two class with identical members, because they have different reference, etc)
2) How do i resize the custom dropdown Editor i created for a property?
3) Can i somehow differentiate between empty value and no value when multiple objects have different values? Like, so
if i have selected 2 objects with same "name" property, it would show name: TEXT
if i have selected 2 objects with "name" = "", it would show name:
if i have selected 2 objects with different "name", it would show name:
4) How do i show something when value is blank? It has to disappear when user starts editing! For example, i have property "name" and if its blank, it should have <> displayed in property grid, but as soon as user clicks to edit, when cursor appears, the <> should disappear
There is a good sample how to implement a custom editor for a property grid in the MSDN:
http://msdn.microsoft.com/en-us/library/ms171840.aspx
and a great article how to realize a custom dropdown, e.g. a dropdown with checklist controls in it on codeproject:
http://www.codeproject.com/Articles/21085/CheckBox-ComboBox-Extending-the-ComboBox-Class-and
If you combine the two articles, you're done ;)
Greetings,

How to add properties in PropertyGrid from the database?

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.

Which types use a FileNameEditor in a PropertyGrid?

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

How do I show like-values in custom fields in a property grid?

I have a property grid that helps me manage all of the controls on a form. These controls are for designer-type folks, so I'm not really worried that much about the user interface... until someone selects multiple objects.
I have a UITypeEditor for the "BottomDiameter" property on these common objects. It keeps track of units (meters vs feet) and does some nice things on-the-fly. However, when someone selects two or three common objects, BottomDiameter is blank, even though it evaluates to the same text string.
The reason (I think) that it is blank is that it is actually three separate objDiameter objects. How can I tell the property grid to behave like all of the other properties and show the value if it evaluates to the same string???
UPDATE: For example, the "Anchor" property has a text output of "Top, Right" but when you pull it down it is an object. Yet, when you select five objects on your form that all have the same Anchor setting you can still see the string "Top, Right" in the property grid.
If your BottomDiameter is a class and not a simple primitive, then you have to override the Equals method in this class.
In the TypeConvertor of the Datatype which is attributed to BottomDiameter Property, you might want to create a vistor like class called say, BottomDiameterVistor which would take an array or a list of the selected BottomDiameter(s). Override the to string property on the BottomDiameterVistor to return your aggregrated text value for the property.

Categories

Resources