3

私は UserControl の例を作成しましたが、まだ (1 日を費やして) 問題を解決できません。

UserControl に複雑なプロパティを持たせて、デザイン時にプロパティ グリッド内でそのプロパティを確認し、変更できるようにしたいと考えています。複雑なプロパティは単純です。1 つの文字列プロパティを持つクラスです。

今、私は2つの問題を抱えています:

1) プロパティ グリッドでテキストの値を変更すると、値は Form1.Designer.cs に移動しません。

2)プロジェクトを再構築するとき(実行する必要がない場合でも)、VSのポップアップが表示され、SettingsCoverterが設定をInstanceDescriptorに変換できないことがあります。これらの設定クラスは私のものです。それを修正するのを手伝ってください。

 [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
}

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);
    }
}
4

1 に答える 1