После нескольких часов работы я решил свою проблему.
Мне нужно было только TypeConverter, чтобы решить эту проблему, поэтому я создал класс, производный от TypeConverter.
class PierListConverter : TypeConverter
{
ArrayList piers = new ArrayList();
public PierListConverter()
{
}
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
// This method returns me the list that will use to fill combo at property grid.
piers.Clear();
foreach (var item in GullsEyeModel.GetInstance().GetPiers())
{
piers.Add(item.Id);
}
StandardValuesCollection cols = new StandardValuesCollection(piers);
return cols;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
// If this method returns true, ConvertFrom method will invoke
if (sourceType == typeof(string))
{
return true;
}
else
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
// In this method i am getting selected text and querying; after that i retrieve proparete Guid value and then returning back to my object that binded property grid.
if (value != null)
{
if (value.ToString() == "Seçiniz")
{
return Guid.Empty;
}
else if (!string.IsNullOrEmpty(value.ToString()))
{
GuidConverter g = new GuidConverter();
PierItem[] pierArray = GullsEyeModel.GetInstance().GetPiers();
PierItem selectedPier = pierArray.Where(item => item.Info.Name == value.ToString()).FirstOrDefault();
if (selectedPier != null)
{
return selectedPier.Id;
}
else
return base.ConvertFrom(context, culture, value);
}
else
return base.ConvertFrom(context, culture, value);
}
else
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
// In this method i am converting ID to string (Name) to display in Property Grid
if (value != null)
{
GuidConverter g = new GuidConverter();
PierItem[] piers = GullsEyeModel.GetInstance().GetPiers();
PierItem selectedPier = piers.Where(item => item.Id== (Guid)g.ConvertFromString(value.ToString())).FirstOrDefault();
if (selectedPier != null)
{
return selectedPier.Info.Name;
}
else
return "Seçiniz";
}
else
return base.ConvertTo(context, culture, value, destinationType);
}
}
Использование пользовательского TypeConverter
class MyClass
{
// my some props..
// my some props..
[TypeConverter(typeof(PierListConverter))]
public Guid PierId {get; set;}
// my some methods..
}