物业电网项目和DoubleClick [英] Property grid item and DoubleClick

查看:150
本文介绍了物业电网项目和DoubleClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PropertyGrid控件编辑某些对象在我的应用程序。我使用的是自定义类型转换器和TypeEditors为更好的用户界面。

I'm using PropertyGrid control for editing some objects in my application. I'm using custom TypeConverters and TypeEditors for better user interface.

我有问题,自定义类型转换器的布尔属性。如果我有这个类:

I have problem with custom TypeConverter for boolean properties. If I have this class:

public class MyClass {
    public string Name { get; set; }

    [System.ComponentModel.TypeConverter( typeof( BoolTypeConverter ) )]
    public bool Flag { get; set; }
}

和创建实例,并将它设置为SelectedObject在PropertyGrid中 - 一切都很好,直到用户DoubleClicked物业电网项目的形式标志的属性。之后的DoubleClick引发此消息:

and I create instance and set it as SelectedObject in PropertyGrid - all is fine until the user DoubleClicked on property grid item form "Flag" property. After DoubleClick is raised this message:

的类型转换器类如下:

public class BoolTypeConverter : System.ComponentModel.TypeConverter {
    public const string TEXT_TRUE = "On";
    public const string TEXT_FALSE = "Off";
    public const string TEXT_NONE = "< none >";

    public override object CreateInstance( System.ComponentModel.ITypeDescriptorContext context, System.Collections.IDictionary propertyValues ) {
        object ret = base.CreateInstance( context, propertyValues );
        return ret;
    }
    public override bool GetCreateInstanceSupported( System.ComponentModel.ITypeDescriptorContext context ) {
        bool ret = base.GetCreateInstanceSupported( context );
        return ret;
    }
    public override bool IsValid( System.ComponentModel.ITypeDescriptorContext context, object value ) {
        bool ret;
        if ( value is string ) {
            string tmpValue = value.ToString().Trim();

            if ( string.Compare( tmpValue, TEXT_NONE, StringComparison.InvariantCultureIgnoreCase ) == 0 ) {
                ret = true;
            }
            else if ( string.Compare( tmpValue, TEXT_TRUE, StringComparison.InvariantCultureIgnoreCase ) == 0 ) {
                ret = true;
            }
            else if ( string.Compare( tmpValue, TEXT_FALSE, StringComparison.InvariantCultureIgnoreCase ) == 0 ) {
                ret = true;
            }
            else {
                bool blValue;
                ret = bool.TryParse( tmpValue, out blValue );
            }
        }
        else {
            ret = base.IsValid( context, value );
        }

        return ret;
    }

    public override bool CanConvertFrom( System.ComponentModel.ITypeDescriptorContext context, Type sourceType ) {
        bool ret = false;
        if ( sourceType == typeof( string ) ) {
            ret = true;
        }
        else {
            ret = base.CanConvertFrom( context, sourceType );
        }

        return ret;
    }
    public override object ConvertFrom( System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value ) {
        object ret = null;

        bool converted = false;
        if ( value is string ) {
            string tmpValue = value.ToString().Trim();
            if ( string.Compare( tmpValue, TEXT_NONE, StringComparison.InvariantCultureIgnoreCase ) == 0
                || string.IsNullOrEmpty( tmpValue ) ) {
                ret = null;
                converted = true;
            }
            else if ( string.Compare( tmpValue, TEXT_TRUE, StringComparison.InvariantCultureIgnoreCase ) == 0 ) {
                ret = true;
                converted = true;
            }
            else if ( string.Compare( tmpValue, TEXT_FALSE, StringComparison.InvariantCultureIgnoreCase ) == 0 ) {
                ret = false;
                converted = true;
            }
            else {
                bool blValue;
                if ( converted = bool.TryParse( tmpValue, out blValue ) ) {
                    ret = blValue;
                }
            }
        }

        if ( false == converted ) {
            ret = base.ConvertFrom( context, culture, value );
        }
        return ret;
    }

    public override bool CanConvertTo( System.ComponentModel.ITypeDescriptorContext context, Type destinationType ) {
        bool ret = false;
        if ( destinationType == typeof( bool ) ) {
            ret = true;
        }
        else {
            ret = base.CanConvertTo( context, destinationType );
        }

        return ret;
    }
    public override object ConvertTo( System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType ) {
        object ret = null;

        bool converted = false;
        if ( destinationType == typeof( string ) ) {
            if ( null == value ) {
                ret = TEXT_NONE;
                converted = true;
            }
            else if ( value is bool? || value is bool ) {
                if ( (bool)value ) { ret = TEXT_TRUE; }
                else { ret = TEXT_FALSE; }

                converted = true;
            }
            else if ( value is string ) {
                ret = value;
                converted = true;
            }
        }
        if ( false == converted ) {
            ret = base.ConvertTo( context, culture, value, destinationType );
        }
        return ret;
    }

    public override StandardValuesCollection GetStandardValues( System.ComponentModel.ITypeDescriptorContext context ) {
        StandardValuesCollection ret;
        Type tpProperty = context.PropertyDescriptor.PropertyType;
        if ( tpProperty == typeof( bool ) ) {
            ret = new StandardValuesCollection( new string[]{
            TEXT_TRUE, TEXT_FALSE
        } );
        }
        else if ( tpProperty == typeof( bool? ) ) {
            ret = new StandardValuesCollection( new string[]{
                TEXT_TRUE, TEXT_FALSE, TEXT_NONE
            } );
        }
        else {
            ret = new StandardValuesCollection( new string[0] );
        }

        return ret;
    }
    public override bool GetStandardValuesSupported( System.ComponentModel.ITypeDescriptorContext context ) {
        bool ret;
        Type tpProperty = context.PropertyDescriptor.PropertyType;
        if ( tpProperty == typeof( bool ) || tpProperty == typeof( bool? ) ) {
            ret = true;
        }
        else {
            ret = false;
        }

        return ret;
    }
}

此行​​为是非常混乱的用户。我怎样才能prevent呢?

This behaviour is very confusing for users. How can I prevent it?

感谢

推荐答案

您GetStandardValues​​()方法是错误的。它必须返回属性的类型,而不是字符串:

Your GetStandardValues() method is wrong. It must return the property type, not strings:

public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
    StandardValuesCollection ret;
    Type tpProperty = context.PropertyDescriptor.PropertyType;

    if (tpProperty == typeof(bool))
        ret = new StandardValuesCollection(new object[] { true, false });
    else if (tpProperty == typeof(bool?))
        ret = new StandardValuesCollection(new object[] { true, false, null });
    else
        ret = new StandardValuesCollection(new object[0]);

    return ret;
}

这篇关于物业电网项目和DoubleClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆