自定义控件类型转换器TypeConverter和ExpandableObjectConverter

一、TypeConverter

  提供一种将当前属性的类型转换为另一种类型的方法。可通过继承TypeConverter自定义类型转换的格式。

  实现自定义类型转换可重写方法:

  CanConvertFrom、ConvertFrom、CanConvertTo、ConvertTo

  定义一个类型Student

 1 public class Student
 2     {
 3         private string no = string.Empty;
 4 
 5         public string NO
 6         {
 7             get
 8             {
 9                 return no;
10             }
11             set
12             {
13                 no = value;
14             }
15         }
16 
17         private string name = string.Empty;
18        
19         public string Name
20         {
21             get
22             {
23                 return name;
24             }
25             set
26             {
27                 name = value;
28             }
29         }
30     }
View Code

  定义一个类型转换器MyTypeConverter,继承TypeConverter,并重写CanConvertFrom、ConvertFrom、CanConvertTo、ConvertTo

 1  /// <summary>
 2     /// 类型转换器
 3     /// </summary>
 4     public class MyTypeConverter : TypeConverter
 5     {
 6         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 7         {
 8             if (typeof(string) == sourceType)
 9             {
10                 return true;
11             }
12             return false;
13         }
14 
15         public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
16         {
17             if (value.GetType() == typeof(string))
18             {
19                 if (string.IsNullOrEmpty(value.ToString()))
20                 {
21                     return new Student();
22                 }
23                 else
24                 {
25                     Student stu = new Student();
26                     string[] split = value.ToString().Split(',');
27                     stu.NO = split[0];
28                     stu.Name = split[1];
29                     return stu;
30                 }
31             }
32             return base.ConvertFrom(context, culture, value);
33         }
34 
35         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
36         {
37             if (destinationType == typeof(string))
38             {
39                 return true;
40             }
41             return base.CanConvertTo(context, destinationType);
42         }
43 
44         public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
45         {
46             if (value.GetType() == typeof(Student) && destinationType == typeof(string))
47             {
48                 Student stu = value as Student;
49                 return stu.NO + "," + stu.Name;
50             }
51             return base.ConvertTo(context, culture, value, destinationType);
52         }
53     }
View Code

     定义一个类型Student的属性,并使用自定义类型转换器MyTypeConverter

 1  private Student _stu;
 2         [Browsable(true)]
 3         [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
 4         [TypeConverter(typeof(MyTypeConverter))]
 5         public Student Stu
 6         {
 7             get
 8             {
 9                 return _stu;
10             }
11             set
12             {
13                 _stu = value;
14             }
15         }
View Code

二、ExpandableObjectConverter

  提供在可扩展对象与其他各种表示形式之间实现转换的类型转换器。

  对属性Stu使用类型转换器ExpandableObjectConverter

  

  对于父节点的显示格式,可以通过重写Student的ToString方法。

原文地址:https://www.cnblogs.com/hedongsong/p/4567223.html