How to get a CF custom editor working
I am working on a custom control library for WM6 smart devices (.net cf 3.5). I created a custom control named GradientPanel, guess what it does. A class called GradientColor describes how the background should be rendered. I already created a custom typecon开发者_JAVA百科verter so that the GradientColor property of a GradientPanel instance is expandable in the Visual Studio property grid. It works fine. I also created a custom editor, derived it from UITypeEditor. I dont want too much of it, just to give a visual clue how the gradient will look like. Here comes the code:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Namespace Drawing.Design
Public Class GradientColorEditor
Inherits System.Drawing.Design.UITypeEditor
Public Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle
Return System.Drawing.Design.UITypeEditorEditStyle.None
End Function
Public Overrides Function GetPaintValueSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Sub PaintValue(ByVal e As System.Drawing.Design.PaintValueEventArgs)
e.Graphics.Clear(Color.White)
Dim value = e.Value
Dim valType As Type = value.GetType()
Dim startColorInfo As System.Reflection.PropertyInfo = valType.GetProperty("StartColor")
Dim startColor As System.Drawing.Color = startColorInfo.GetValue(value, Nothing)
Dim endColorInfo As System.Reflection.PropertyInfo = valType.GetProperty("EndColor")
Dim endColor As System.Drawing.Color = endColorInfo.GetValue(value, Nothing)
Dim fillDirectionInfo As System.Reflection.PropertyInfo = valType.GetProperty("FillDirection")
Dim fillDirType As Type = fillDirectionInfo.PropertyType
Dim fillDirNames() As String = [Enum].GetNames(fillDirType)
Dim fillDirValues() As Integer = [Enum].GetValues(fillDirType)
Dim fillDirValNameDict As New System.Collections.Generic.Dictionary(Of Integer, String)()
For cnt As Integer = 0 To fillDirValues.Length - 1
fillDirValNameDict.Add(fillDirValues(cnt), fillDirNames(cnt))
Next
Dim fillDir As Integer = fillDirectionInfo.GetValue(value, Nothing)
If startColor = System.Drawing.Color.Transparent OrElse endColor = System.Drawing.Color.Transparent Then
Exit Sub
End If
Dim brush As New LinearGradientBrush(e.Bounds, startColor, endColor, _
If(fillDir = 0, LinearGradientMode.Horizontal, LinearGradientMode.Vertical))
e.Graphics.FillRectangle(brush, e.Bounds)
End Sub
End Class
End Namespace
It does compile and the reflection part is the same that is used (and is working) in the custom typeconverter. However I simply can't get Visual Studio to use this editor, and it is really driving me crazy. Here is the relevant part of the XMTA file:
<Class Name="Tgz.Controls.GradientPanel">
<DesktopCompatible>true</DesktopCompatible>
<Property Name="GradientColor">
<TypeConverter>
Tgz.Drawing.Design.GradientColorConverter, Tgz.Drawing.Design,
Version=0.1.0.0, Culture=neutral, PublicKeyToken=3f315c03f85ce5c1
</TypeConverter>
<Browsable>true</Browsable>
<Category>Appearance</Category>
<Description>Defines the gradient background of the control.</Description>
<EditorBrowsable>true</EditorBrowsable>
<Editor>
<Type>
Tgz.Drawing.Design.GradientColorEditor, Tgz.Drawing.Design,
Version=0.1.0.0, Culture=neutral, PublicKeyToken=3f315c03f85ce5c1
</Type>
<BaseType>
System.Drawing.Design.UITypeEditor, System.Drawing.Design,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
</BaseType>
</Editor>
</Property>
</Class>
So how come Visual Studio takes the <TypeConverter> tag into account, but does not care about the <Editor> tag? Or am I missing something?
Please, help.
Thanks, TGZ
My bad, the System.Drawing.Design.UITypeEditor class is not in the System.Drawing.Design, but the System.Drawing assembly, looks like thats why it did not find my editor. Well, actually it did find it, but it didn't find the base class.
I tested it though, and it looks like the line
e.Graphics.Clear(Color.White)
which is the first line of the PaintValue(...) procedure is not needed. In fact it clears all the preceding lines in the property grid, so it should be avoided.
Just how in the world did you inherit from System.ComponentModel.TypeConverter? The mentioned TypeConverter is just an empty class. The class System.Drawing.Design.UITypeEditor is also not available on the compact framework.
How did you do it?
精彩评论