C# 属性网格项并双击
我正在使用PropertyGrid控件编辑应用程序中的一些对象。我正在使用自定义类型转换器和类型编辑器,以获得更好的用户界面 我对布尔属性的自定义类型转换器有问题。如果我有这门课:C# 属性网格项并双击,c#,.net,winforms,type-conversion,propertygrid,C#,.net,Winforms,Type Conversion,Propertygrid,我正在使用PropertyGrid控件编辑应用程序中的一些对象。我正在使用自定义类型转换器和类型编辑器,以获得更好的用户界面 我对布尔属性的自定义类型转换器有问题。如果我有这门课: public class MyClass { public string Name { get; set; } [System.ComponentModel.TypeConverter( typeof( BoolTypeConverter ) )] public bool Flag { ge
public class MyClass {
public string Name { get; set; }
[System.ComponentModel.TypeConverter( typeof( BoolTypeConverter ) )]
public bool Flag { get; set; }
}
我创建实例并在PropertyGrid中将其设置为SelectedObject-在用户双击属性网格项表单“Flag”属性之前,一切都正常。双击后出现此消息:(来源:) TypeConverter类看起来:
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;
}
}
公共类BoolTypeConverter:System.ComponentModel.TypeConverter{
public const string TEXT_TRUE=“On”;
public const string TEXT_FALSE=“Off”;
public const string TEXT_NONE=“”;
公共重写对象CreateInstance(System.ComponentModel.ITypeDescriptorContext上下文,System.Collections.IDictionary PropertyValue){
object ret=base.CreateInstance(上下文、属性值);
返回ret;
}
公共覆盖布尔GetCreateInstanceSupported(System.ComponentModel.ITypeDescriptorContext上下文){
bool ret=base.GetCreateInstanceSupported(上下文);
返回ret;
}
公共覆盖布尔值有效(System.ComponentModel.ITypeDescriptorContext上下文,对象值){
布尔-雷特;
if(值为字符串){
字符串tmpValue=value.ToString().Trim();
if(string.Compare(tmpValue,TEXT_NONE,StringComparison.InvariantCultureIgnoreCase)==0){
ret=真;
}
else if(string.Compare(tmpValue,TEXT_TRUE,StringComparison.InvariantCultureIgnoreCase)==0){
ret=真;
}
else if(string.Compare(tmpValue,TEXT_FALSE,StringComparison.InvariantCultureIgnoreCase)==0){
ret=真;
}
否则{
布尔值;
ret=bool.TryParse(tmpValue,out blValue);
}
}
否则{
ret=base.IsValid(上下文,值);
}
返回ret;
}
公共覆盖布尔CanConvertFrom(System.ComponentModel.ITypeDescriptorContext上下文,类型sourceType){
bool-ret=假;
if(sourceType==typeof(string)){
ret=真;
}
否则{
ret=base.CanConvertFrom(上下文,源类型);
}
返回ret;
}
公共重写对象ConvertFrom(System.ComponentModel.ITypeDescriptorContext上下文,System.Globalization.CultureInfo区域性,对象值){
object ret=null;
bool=false;
if(值为字符串){
字符串tmpValue=value.ToString().Trim();
if(string.Compare(tmpValue,TEXT_NONE,StringComparison.InvariantCultureIgnoreCase)==0
||string.IsNullOrEmpty(tmpValue)){
ret=null;
转换=真;
}
else if(string.Compare(tmpValue,TEXT_TRUE,StringComparison.InvariantCultureIgnoreCase)==0){
ret=真;
转换=真;
}
else if(string.Compare(tmpValue,TEXT_FALSE,StringComparison.InvariantCultureIgnoreCase)==0){
ret=假;
转换=真;
}
否则{
布尔值;
if(转换后的=bool.TryParse(tmpValue,out blValue)){
ret=BL值;
}
}
}
if(false==已转换){
ret=base.ConvertFrom(上下文、文化、值);
}
返回ret;
}
公共覆盖布尔CanConvertTo(System.ComponentModel.ITypeDescriptorContext上下文,类型destinationType){
bool-ret=假;
if(destinationType==typeof(bool)){
ret=真;
}
否则{
ret=base.CanConvertTo(上下文,destinationType);
}
返回ret;
}
公共重写对象转换器(System.ComponentModel.ITypeDescriptorContext上下文,System.Globalization.CultureInfo区域性,对象值,类型destinationType){
object ret=null;
bool=false;
if(destinationType==typeof(string)){
if(null==值){
ret=文本\无;
转换=真;
}
else if(值为bool?| |值为bool){
如果((bool)值){ret=TEXT_TRUE;}
else{ret=TEXT_FALSE;}
转换=真;
}
else if(值为字符串){
ret=价值;
转换=真;
}
}
if(false==已转换){
ret=base.ConvertTo(上下文、区域性、值、destinationType);
}
返回ret;
}
公共覆盖标准值集合GetStandardValues(System.ComponentModel.ITypeDescriptor上下文){
标准值收集率;
类型tpProperty=context.PropertyDescriptor.PropertyType;
if(tpProperty==typeof(bool)){
ret=新标准值集合(新字符串[]){
TEXT\u TRUE,TEXT\u FALSE
} );
}
else if(tpProperty==typeof(bool?){
ret=新标准值集合(新字符串[]){
TEXT\u TRUE、TEXT\u FALSE、TEXT\u NONE
} );
}
否则{
ret=新标准值集合(新字符串[0]);
}
返回ret;
}
公共覆盖布尔GetStandardValuesSupported(System.ComponentModel.ITypeDescriptor上下文){
布尔-雷特;
类型tpProperty=context.PropertyDescriptor.PropertyType;
如果(tpProperty==typeof(bool)| | tpProperty==typeof(bool?){
ret=真;
}
否则{
ret=假;
}
返回r
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;
}