Reputation: 20464
SCENARIO
I've sub-classed a control and I would like to add an StringFormat
expandable group in the control's property grid to set its values at design time.
PROBLEM
I'm unable to properly add the StringFormat properties into the property grid, this is what I get:
QUESTION
Which is the simplest way to implement an StringFormat
into a PropertyGrid
?
CODE
I've tried the suggestion of @Plutonix answer here: Create an expandable group in a property grid? (sure I'm doing something wrong)
I preffer to avoid a TypeConverter
usage in case of this could be done in a simpler way, because I'm not sure whether this implementation needs this kind of headache.
Public Class MyControl: Inherits ListBox
Public Property MyProperty As MyStringFormat = New MyStringFormat
End Class
<TypeConverter(GetType(StringFormatConverter))>
Public Class MyStringFormat
<Browsable(True)>
<NotifyParentProperty(True)>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Property myStringFormat As StringFormat
Public Sub New()
' default values, if any
myStringFormat = New StringFormat
End Sub
End Class
Public Class StringFormatConverter : Inherits ExpandableObjectConverter
Public Overrides Function ConvertTo(context As ITypeDescriptorContext,
culture As Globalization.CultureInfo,
value As Object,
destinationType As Type) As Object
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class
Upvotes: 1
Views: 509
Reputation: 38865
Following on with your earlier adventure, this is all you need:
' property on the control
<Browsable(True), EditorBrowsable(EditorBrowsableState.Always),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
DefaultValue(-1)>
Public Property TextFormating As TextFormat
' be sure to instance it!
' TextFormating = New TextFormat
Using the Component approach:
Public Class TextFormat
Inherits Component
<Browsable(True), NotifyParentProperty(True),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always), DefaultValue(-1)>
Public Property Alignment As StringAlignment
<Browsable(True), NotifyParentProperty(True),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always), DefaultValue(-1)>
Public Property LineAlignent As StringAlignment
<Browsable(True), NotifyParentProperty(True),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always), DefaultValue(-1)>
Public Property Trimming As StringTrimming
<Browsable(True), NotifyParentProperty(True),
Editor(GetType(UIEnumEditor), GetType(UITypeEditor)),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always), DefaultValue(-1)>
Public Property FormatFlags As StringFormatFlags
End Class
Add whatever other properties you want. You can inherit because it is a sealed class, also it doesnt have all the attributes you require. You wont like the default behavior of the StringFormatFlags
property; and if you think a TypeConverter is a headache, you are in for a treat!
Since StringFormatFlags
is a Flag Enum, you will likely want to pick and combine several at a time. The default UITypeEditor though is a DropDown, but we want a DropDownCheckList. For this, we need a custom UITypeEditor. These are not as scary as they sounds, but there is also no need to recreate the wheel:
The Enhanced CollectionEditor Framework article on CodeProject is a collection editor framework which also includes a EnumTypeEditor (scroll to the end of the article where it is mentioned in passing). In about the middle of the article is a brief primer on TypeConverters
as well.
To use it:
- download the file and include the DLL in your project
- then (re)decorate your TextFormat.TextFormatFlags
property:
<Editor(GetType(UIEnumEditor), GetType(UITypeEditor)),
Browsable(True), NotifyParentProperty(True),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always), DefaultValue(0)>
Public Property TextFormatFlags As StringFormatFlags
(Note the added UIEnumEditor
attribute).
The article and demo shows how to inherit the base editor to customize a few things. One thing it will do is use Descriptions
rather than the Enum Name in the list, if they exist and if you want. That's it, 2 steps and you can check off which flags to combine:
You dont need a TypeConverter for this one because combining 4 properties into one string doesnt make much sense. As before, it will still have the empty/extra drop down when your Type inherits Component.
Upvotes: 2