【发布时间】:2018-12-18 10:12:08
【问题描述】:
正如标题所说,我注意到当类“T”的所有属性都是只读的时,类别没有显示在集合(T 的默认集合编辑器中)的 **PropertyGrid*(在其默认集合编辑器中) .
下面的代码代表我的代码结构:
C#:
[TypeConverter(typeof(ExpandableObjectConverter))]
public class TestClass1 {
public TestClass2 TestProperty1 {get;} = new TestClass2();
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public sealed class TestClass2 {
[TypeConverter(typeof(CollectionConverter))]
public ReadOnlyCollection<TestClass3> TestProperty2 {
get {
List<TestClass3> collection = new List<TestClass3>();
for (int i = 0; i <= 10; i++) {
collection.Add(new TestClass3());
}
return collection.AsReadOnly();
}
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public sealed class TestClass3 {
[Category("Category 1")]
public string TestProperty3 {get;} = "Test";
}
VB.NET:
<TypeConverter(GetType(ExpandableObjectConverter))>
Public Class TestClass1
Public ReadOnly Property TestProperty1 As TestClass2 = New TestClass2()
End Class
<TypeConverter(GetType(ExpandableObjectConverter))>
Public NotInheritable Class TestClass2
<TypeConverter(GetType(CollectionConverter))>
Public ReadOnly Property TestProperty2 As ReadOnlyCollection(Of TestClass3)
Get
Dim collection As New List(Of TestClass3)
For i As Integer = 0 To 10
collection.Add(New TestClass3())
Next
Return collection.AsReadOnly()
End Get
End Property
End Class
<TypeConverter(GetType(ExpandableObjectConverter))>
Public NotInheritable Class TestClass3
<Category("Category 1")>
Public ReadOnly Property TestProperty3 As String = "Test"
End Class
问题在于 TestProperty3。只读时,属性网格中不显示类别(“类别 1”)...
但如果我将属性设置为可编辑,则显示类别...
C:#
[Category("Category 1")]
public string TestProperty3 {get; set;} = "Test";
VB.NET:
<Category("Category 1")>
Public Property TestProperty3 As String = "Test"
不仅如此,让我们假设在 TestClass3 中声明了 10 个属性(而不是本示例中的 1 个),其中 9 个是只读的,1 个是可编辑的,那么,在这种情况下会显示所有类别。另一方面,如果所有 10 个属性都是只读的,则不会显示类别。
PeopertyGrid 的这种行为对我来说非常烦人和意外。我希望看到我的自定义类别,无论在我的类中是否声明了带有 setter 的属性。
我必须显示哪些类别具有我的类的所有属性都是只读的?也许编写自定义 TypeConverter 或集合编辑器可以解决这种烦人的视觉表现行为?
【问题讨论】:
-
查看这个答案:How to set IsReadOnly for a property for a usercontrol。您可以使用它来更改/fake 具有 empty
set的属性的只读方面(有一个设置器,但它实际上什么也没设置)。也许这不是完美的解决方案,但它可能很有用。 -
有了 它可能很有用 我的意思是提供的解决方案有一些特殊的实现,可用于自定义
TypeDescriptionProvider,添加一些缺失的功能。
标签: c# .net vb.net generics propertygrid