首页 > 解决方案 > 允许在可浏览属性的组合框中复制/粘贴

问题描述

我有一个具有复杂可浏览属性的控件,它在表单中显示该类型的所有控件的列表,但我想允许在组合框中复制和粘贴控件的名称。我使用了 a TypeConverter,但组合框的项目消失了。还有另一种方法吗?或者我如何返回GetStandardValues函数中的默认项目列表?我试图返回基本函数,但它不起作用。

public class SubflowchartConverter : TypeConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        FlowChart flowchart = context.Instance as FlowChart;

        if (flowchart != null)
            return true;
        else
            return base.GetStandardValuesSupported(context);
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return false;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return base.GetStandardValues(context);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;

        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is string)
        {
            string flowchartName = value.ToString();

            if (flowchartName == "(none)")
                return null;

            FlowChart flowchart = context.Instance as FlowChart;

            if (flowchart.Parent.Controls.ContainsKey(flowchartName))
            {
                return flowchart.Parent.Controls[flowchartName];
            }
        }

        return base.ConvertFrom(context, culture, value);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string))
            return true;

        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            if (value == null)
                return "(none)";

            FlowChart flowchart = value as FlowChart;

            if (flowchart != null)
                return flowchart.Name;
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }
}

标签: c#typeconverter

解决方案


推荐阅读