If I write a WinForms visual control and want to make one of its properties of a value type nullable, do I need to create a special type converter or editor for it to make it fully usable in the VS Property Grid?
For example, .NET Framework provides the NullableConverter class. Do I need to attach it to my nullable property via the TypeConverterAttribute?
I tried to convert one of the int
properties to int?
, and it seems this property works fine in the Property Grid. I can specify a numeric value or clear this property to make it null again.
The property grid uses TypeDescriptor to gets its converter information. So for example, if you have this class:
public class MyClass
{
public int? NullableInteger { get; set; }
}
With this code:
// get first property's type converter
var cv = TypeDescriptor.GetProperties(typeof(MyClass))[0].Converter;
You will get the TypeConverter
for the NullableInteger
property, which is already NullableConverter
.
So, you don't have to declare it manually.