艾伟:WinForm控件开发总结(五)-----为控件的复杂属性提供类型转换器

简介: 上一篇文章我已经介绍了TypeConverterAttribute元数据的作用,本文将通过代码向你展示具体的实现。在这个例子中,我要给控件添加一个复杂的属性,这个属性对这个控件没有什么功用,纯粹是为了演示,有些牵强附会了。

      上一篇文章我已经介绍了TypeConverterAttribute元数据的作用,本文将通过代码向你展示具体的实现。在这个例子中,我要给控件添加一个复杂的属性,这个属性对这个控件没有什么功用,纯粹是为了演示,有些牵强附会了。
       现在在前一篇文章中的创建的控件代码中添加一个Scope属性:
      

        [Browsable(true)]
        public Scope Scope
         {
            get
             {
                return _scope;
            }
            set
             {
                _scope = value;
            }
        }
      这个属性的类型是Scope类,代码如下:
public class Scope
     {
        private Int32 _min;
        private Int32 _max;

        public Scope()
         {
        }

public Scope(Int32 min, Int32 max)
         {
            _min = min;
            _max = max;
        }

        [Browsable(true)]
        public Int32 Min
         {
            get
             {
                return _min;
            }
            set
             {
                _min = value;
            }
        }

        [Browsable(true)]
        public Int32 Max
         {
            get
             {
                return _max;
            }
            set
             {
                _max = value;
            }
           
        }
}

       添加完属性后,build控件工程,然后在测试的工程里选中添加的控件,然后在属性浏览器里观察它的属性,发现Scope属性是灰的,不能编辑。前一篇文章提到了,在属性浏览器里可以编辑的属性都是有类型转换器的,而.NET框架为基本的类型和常用的类型都提供了默认的类型转换器。接下来我们为Scope类添加一个类型转换器,以便这个属性能够被编辑,而且也可以在源代码文件里自动生成相应的代码。下面是类型转换器的代码:

public class ScopeConverter : TypeConverter
     {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
         {
            if (sourceType == typeof(String)) return true;

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
         {
            if (destinationType == typeof(String)) return true;

            if (destinationType == typeof(InstanceDescriptor)) return true;

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
         {
            String result = "";
            if (destinationType == typeof(String))
             {
                Scope scope = (Scope)value;
                result = scope.Min.ToString()+"," + scope.Max.ToString();
                return result;

            }

            if (destinationType == typeof(InstanceDescriptor))
             {
                ConstructorInfo ci = typeof(Scope).GetConstructor(new Type[]  {typeof(Int32),typeof(Int32) });
                Scope scope = (Scope)value;
                return new InstanceDescriptor(ci, new object[]  { scope.Min,scope.Max });
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
         {
            if (value is string)
             {
                String[] v = ((String)value).Split(',');
                if (v.GetLength(0) != 2)
                 {
                    throw new ArgumentException("Invalid parameter format");
                }

                Scope csf = new Scope();
                csf.Min = Convert.ToInt32(v[0]);
                csf.Max = Convert.ToInt32(v[1]);
                return csf;
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

      现在我们为类型提供类型转换器,我们在类型前面添加一个TypeConverterAttribute,如下:
           
    [TypeConverter(typeof(ScopeConverter))]
    public class Scope
        添加完以后build工程,然后切换到测试工程,选中控件,在属性浏览器里查看属性,现在的Scope属性可以编辑了,如下图所示: 
         
         我们修改默认的值,然后看看Form设计器为我们生成了什么代码:
      
this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.myListControl1.Item.Add(1);
            this.myListControl1.Item.Add(2);
            this.myListControl1.Item.Add(3);
            this.myListControl1.Item.Add(6);
            this.myListControl1.Item.Add(8);
            this.myListControl1.Item.Add(9);
            this.myListControl1.Location = new System.Drawing.Point(12, 34);
            this.myListControl1.Name = "myListControl1";
            this.myListControl1.Scope = new CustomControlSample.Scope(10, 200);
            this.myListControl1.Size = new System.Drawing.Size(220, 180);
            this.myListControl1.TabIndex = 1;
        this.myListControl1.Text = "myListControl1";
        关键是这一行this.myListControl1.Scope = new CustomControlSample.Scope(10, 200),Scope类的类型转换器为属性提供了实例化的代码。      
目录
相关文章
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
88 0
艾伟:WinForm控件开发总结(六)-----控件属性类型转换器代码详解
在上一篇文章,我为控件添加一个一个复杂属性,并且为这个属性的类型的编写了一个类型转换器,现在我们来看看这个类型转换器的代码,并解释一下这些代码的意义。       要实现一个类型转换器,我们必须要重写(override)四个方法:       CanConvertFrom()――根据类型参数进行测试,判断是否能从这个类型转换成当前类型,在本例中我们只提供转换string和InstanceDescriptor类型的能力。
841 0
|
Web App开发
艾伟:WinForm控件开发总结(三)------认识WinForm控件常用的Attribute
在前面的文章里我们制作了一个非常简单的控件。现在我们回过头来看看这些代码透露出什么信息。   这个类是直接从Control类派生出来的,自定义控件都是直接从Control类派生出来的。这个类定义了一个属性TextAlignment,用来控制文本在控件中显示的位置:           ...
1026 0
|
Web App开发
艾伟:WinForm控件开发总结(七)-----为复杂属性的子属性提供编辑功能
前面的几篇文章中,我们给控件添加一个复杂的类型Scope,并且给它的类型提供的一个类型转换器,现在我们可以在属性浏览器中编辑它的值,并且它的值也被串行化的源代码里了。但是你有没有发现,在属性浏览器里编辑这个属性的值还是不太方便。
703 0
|
C# 前端开发
WPF Adorner+附加属性 实现控件友好提示
原文:WPF Adorner+附加属性 实现控件友好提示 标题太空泛,直接上图   无论是在验证啊,还是提示方面等一些右上角的角标之类的效果,我们会怎么做? 这里介绍一种稍微简单一些的方法,利用附加属性和Adorner来完成。
1018 0
|
C#
WPF中一个控件绑定另一个控件的属性
原文:WPF中一个控件绑定另一个控件的属性 如同一个Grid中的一个按钮根据另一个按钮的显示与否作出不同的响应: 绑定的时候通过ElementName来指定控件               ...
1876 0
|
Android开发
QTQuick控件基础(2)
import QtQuick 2.2import QtQuick.Controls 1.2import QtQuick.Window 2.1ApplicationWindow {    visible: true    width: 640    height: 480    title: qsTr...
1023 0
|
容器 数据可视化
QTQuick控件基础(1)
一、ItemQtQuick所有的可视项目都继承自Item,它定义了可视化项目所有通用特性(x\y\width\height\anchors等)具体包括 1、作为容器 2、不透明性 没有设置opacity属性 设置了opacity属性为0.5 当然也可以这样写 3、visible属性用来设置项目是否可见。
1122 0

热门文章

最新文章