Type Editors
So far you've seen how type converters can convert various data types to strings for representation in the Properties window. But some data types don't rely on string editing at all. For example, if you need to set an enumerated value (such as BorderStyle), you can choose from a drop-down list of all the values in the enumeration. More impressively, if you need to set
a color, you can choose from a drop-down color picker. And some properties have the ability to break out of the Properties window altogether. One example is the Columns property of the GridView. If you click the ellipsis next to the property name, a dialog box will appear where you can configure the column collection using a rich user interface.
These properties all rely on UI type editors. Type editors have a single task in life—they generate user interfaces that allow you to set control properties more conveniently. Certain data types (such as collections, enumerations, and colors) are automatically associated with advanced type editors. In other cases, you might want to create your own type editor classes from scratch. All UI type editors are located in the System.Drawing.Design namespace.
Just as with type converters (and almost everything in the extensible architecture of .NET design-time support), creating a new type editor involves inheriting a base class (in this case UITypeEditor) and overriding desired members. The methods you can override include the following:
GetEditStyle(): Specifies whether the type editor is a DropDown (provides a list of specially drawn choices), Modal (provides a dialog box for property selection), or None (no editing supported).
EditValue(): This method is invoked when a property is edited (for example, the ellipsis next to the property name is clicked in the Properties window). Generally, this is where you would create a special dialog box for property editing.
GetPaintValueSupported(): Use this to return true if you are providing a PaintValue() implementation.
PaintValue(): Invoked to paint a graphical thumbnail that represents the value in the property grid. For example, this is used to create the color box for color properties.
The code for UI type editors isn't overly complicated, but it can take a bit of getting used to for web developers. That's because it involves using the other user interface platform in .NET—Windows Forms. Although the topic of Windows Forms is outside the scope of this book, you can learn a lot from a basic example. Figure 28-7 shows a custom color editing control that allows you to set various components of a color independently using sliders. As you do, it displays the color in a box at the bottom of the control.
- Figure 28-7. Usinga custom type editor
The code for the actual control (the ColorTypeEditorControl) isn't shown here, but you can refer to the downloadable examples for this chapter to take a closer look. However, the full code for the type editor that uses this control is as follows:
Public Class ColorTypeEditor Inherits UITypeEditor Public Overrides Function GetEditStyle _ (ByVal context As ITypeDescriptorContext) _ As UITypeEditorEditStyle
' This editor appears when you click a drop-down arrow. Return UITypeEditorEditStyle.DropDown End Function
Public Overrides Function EditValue _ (ByVal context As ITypeDescriptorContext, _
ByVal provider As IServiceProvider, ByVal value As Object) As Object Dim srv As IWindowsFormsEditorService = Nothing
' Get the editor service from the provider, ' which you need to create the drop-down window. If provider IsNot Nothing Then srv = DirectCast(provider.GetService(GetType _ (IWindowsFormsEditorService)), IWindowsFormsEditorService) End If
If srv IsNot Nothing Then
' Create an instance of the custom Windows Forms
' color-picking control.
' Pass the current value of the color.
Dim editor As New ColorTypeEditorControl(DirectCast _
(value, System.Drawing.Color), _
TryCast(context.Instance, WebControl))
' Show the control. srv.DropDownControl(editor)
' Return the selected color information. Return editor.SelectedColor
Else
' Return the current value. Return value End If End Function
Public Overrides Function GetPaintValueSupported _ (ByVal context As ITypeDescriptorContext) As Boolean
' This type editor will generate a color box thumbnail. Return True End Function
Public Overrides Sub PaintValue(ByVal e As PaintValueEventArgs) ' Fills the left rectangle with a color.
Dim control As WebControl = TryCast(e.Context.Instance, WebControl) e.Graphics.FillRegion(New SolidBrush _ (control.BackColor), New Region(e.Bounds)) End Sub End Class
To create this example, you need to add a reference to the System.Windows.Forms assembly. It also helps to import the System.Windows.Forms.Design namespace, which contains the IWindowsFormsEditorService interface.
To use this type editor, you need to attach it to a property that uses the Color data type. Most web controls already include color properties, but you can override one of them and apply a new Editor attribute.
Here's an example that does exactly that to attach the type editor to the BackColor property of the RichLabel control:
<Editor(GetType(ColorTypeEditor), GetType(UITypeEditor))> _ Public Overrides Property BackColor() As Color Get
Return MyBase.BackColor End Get Set
MyBase.BackColor = value End Set End Property
Post a comment