I made an example of UserControl and still (spent whole day) unable to fix my problem.
I want UserControl to have a complex property, to see that property inside property grid at design-time, able to change it so on. Complex property is simple - its a class with one string property.
Now i have 2 problems:
1) When i change the value of text at property grid - the value dont goes to Form1.Designer.cs
2) Sometimes when i rebuild project (even dont need to run) i have a VS's popup where said SettingsCoverter is unable to convert Settings to InstanceDescriptor. Those Settings classes are mine. Please help to fix that.
[TypeConverter(typeof(SettingsConverter))]
public class Settings : INotifyPropertyChanged
{
private string stringText = "123";
public string StringText
{
get { return stringText; }
set
{
stringText = value;
OnPropertyChanged("StringText");
}
}
public Settings()
{
}
public Settings(string fText)
{
StringText = fText;
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(name));
}
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
and
class SettingsConverter : ExpandableObjectConverter
{
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
return new Settings((string)propertyValues["StringText"]);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value is Settings)
{
if (destinationType == typeof(InstanceDescriptor))
{
Settings settings = (Settings)value;
object[] properties = new object[1];
Type[] types = new Type[1];
types[0] = typeof(string);
properties[0] = settings.StringText;
ConstructorInfo ci = typeof(Settings).GetConstructor(types);
return new InstanceDescriptor(ci, properties);
}
if (destinationType == typeof(string))
{
Settings settings = (Settings)value;
return settings.StringText;
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value == null)
return "";
if (value is string)
return new Settings(value as string);
return base.ConvertFrom(context, culture, value);
}
}