• 自定义控件类型转换器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方法。

  • 相关阅读:
    P4049 [JSOI2007]合金
    CF1073C Vasya and Robot
    输出100以内奇数,偶数,质数,合数的脚本
    取/etc/password文件最后一个单词的最后一个字符
    window下进程退出后自动重启
    如何让DOS命令在新窗口打开
    dos命令关闭所有dos窗口
    使用jps查看JVM进程信息
    windows .bat批处理实现进程监控确保程序运行
    经典博客4(六尺帐篷)
  • 原文地址:https://www.cnblogs.com/hedongsong/p/4567223.html
Copyright © 2020-2023  润新知