jkhk (owner)

Revisions

gist: 245263 Download_button fork
public
Public Clone URL: git://gist.github.com/245263.git
Embed All Files: show embed
C# #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.ComponentModel;
using JoeK.Resources;
 
namespace JoeK.Converters
{
    public class YesNoConverter : BooleanConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }
 
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
        }
 
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value.GetType() == typeof(string))
            {
                if (((string)value) == LocalisableStrings.Yes)
                {
                    return true;
                }
                if (((string)value) == LocalisableStrings.No)
                {
                    return false;
                }
 
                throw new Exception("Values must be \"Yes\" or \"No\"");
            }
            return base.ConvertFrom(context, culture, value);
        }
 
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return (((bool)value) ? LocalisableStrings.Yes : LocalisableStrings.No);
            }
 
            return base.ConvertTo(context, culture, value, destinationType);
        }
 
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            bool[] bools = new[] { true, false };
            StandardValuesCollection svc = new StandardValuesCollection(bools);
            return svc;
        }
    }
}