Bạn sẽ phải tùy chỉnh nó. Như thế này:
class YesNoConverter : BooleanConverter {
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
if (value is bool && destinationType == typeof(string)) {
return values[(bool)value ? 1 : 0];
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
string txt = value as string;
if (values[0] == txt) return false;
if (values[1] == txt) return true;
return base.ConvertFrom(context, culture, value);
}
private string[] values = new string[] { "No", "Yes" };
}
mẫu sử dụng:
class MyControl : Control {
[TypeConverter(typeof(YesNoConverter))]
public bool Prop { get; set; }
}
Bạn không nhận được sự giúp đỡ từ System.Globalization để làm công việc này bằng các ngôn ngữ khác.
Nguồn
2011-01-12 14:07:33
Cảm ơn, đóng, nhưng khi tôi nhấp đúp vào giá trị, tôi nhận được lỗi Dlg "Giá trị thuộc tính không hợp lệ"/"Đối tượng thuộc loại 'System.String' không thể chuyển đổi thành loại 'System.Boolean'." –
Ugh, pita. Đặt một PropertyGrid trên một biểu mẫu để gỡ lỗi này khi chạy. –
Bạn có thể cần Hệ thống :: Chuyển đổi :: ToBoolean (giá trị) thay vì truyền sang bool. – ThePragmatist