2013-09-16 2 views
1

Почему в мире появляется следующее свойство только для чтения в PropertyGrid?Почему моя собственность появляется только что прочитанная

Public Property Location() As PointF 
    Get 
     Return New PointF(mLeft, mTop) 
    End Get 
    Set(ByVal value As PointF) 
     mLeft = value.X 
     mTop = value.Y 
    End Set 
End Property 

, а следующее свойство одного и того же объекта появляется просто отлично (чтение/запись):

Public Property Size() As SizeF 
    Get 
     Return New SizeF(mWidth, mHeight) 
    End Get 
    Set(ByVal value As SizeF) 
     mWidth = value.Width 
     mHeight = value.Height 
    End Set 
End Property 

На самом деле PropertyGrid отображает версию моего первого свойства ToString(), то есть значение равно как {X = 103, Y = 235} для Location.

ответ

1

Поскольку SizeF имеет значение по умолчанию TypeConverter определено:

[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true), TypeConverter(typeof(SizeFConverter))] 
public struct SizeF 
{ 
    ... 
} 

Хотя PointF не имеет:

[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)] 
public struct PointF 
{ 
    .... 
} 
+0

Не мог найти это самостоятельно. Бесконечно благодарен. – dotNET

1

Для кого-то стучал головой о стену, здесь PointFConverter класс:

Imports System.ComponentModel 

Public Class PointFConverter 
    Inherits ExpandableObjectConverter 

    Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean 
     If sourceType = GetType(String) Then 
      Return True 
     Else 
      Return MyBase.CanConvertFrom(context, sourceType) 
     End If 
    End Function 

    Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As System.Globalization.CultureInfo, value As Object) As Object 
     If TypeOf value Is String Then 
      Try 
       Dim s As String = DirectCast(value, String) 
       Dim converterParts As String() = s.Split(","c) 
       Dim x As Single = 0.0F 
       Dim y As Single = 0.0F 
       If converterParts.Length > 1 Then 
        x = Single.Parse(converterParts(0).Trim()) 
        y = Single.Parse(converterParts(1).Trim()) 
       ElseIf converterParts.Length = 1 Then 
        x = Single.Parse(converterParts(0).Trim()) 
       End If 
       Return New PointF(x, y) 
      Catch 
       Throw New ArgumentException("Cannot convert [" + value.ToString() + "] to pointF") 
      End Try 
     End If 
     Return MyBase.ConvertFrom(context, culture, value) 
    End Function 

    Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As System.Globalization.CultureInfo, value As Object, destinationType As Type) As Object 
     If destinationType = GetType(String) Then 
      If value.[GetType]() = GetType(PointF) Then 
       Dim pt As PointF = DirectCast(value, PointF) 
       Return String.Format("{0}, {1}", pt.X, pt.Y) 
      End If 
     End If 

     Return MyBase.ConvertTo(context, culture, value, destinationType) 
    End Function 
End Class 

Просто примените его к вашей собственности следующим образом:

<TypeConverter(GetType(PointFConverter))> _ 
Public Property Location() As PointF 
    Get 
     Return New PointF(mLeft, mTop) 
    End Get 
    Set(ByVal value As PointF) 
     mLeft = value.X 
     mTop = value.Y 
    End Set 
End Property 
Смежные вопросы