You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeoSigma.SigmaDrawerStyle.Converter
{
public class YesNoConverter : BaseDropDownConverter
{
private static readonly string Yes = "是";
private static readonly string No = "否";
private static volatile StandardValuesCollection values = new StandardValuesCollection(new object[2]
{
true, false
});
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return values;
}
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)
{
if (value.Equals(YesNoConverter.Yes))
return true;
if (value.Equals(YesNoConverter.No))
return false;
}
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 is bool bValue)
return bValue ? YesNoConverter.Yes : YesNoConverter.No;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}