C#自定义控件绑定IList泛型、DataTable、DataSet数据源

来源:互联网 发布:java事务的四个特性 编辑:程序博客网 时间:2024/05/22 03:09


自定义控件是可重用的用户界面功能组件,它们封装了用户界面功能,并且可以用于客户端 Windows 应用程序。自定义控件可以组合现有控件、扩展现有控件或创作自己的自定义控件。我今天封装一个自定义控件,实现接收井的信息(井名、井的位置坐标)进行绘图。井信息的数据源可能是IList泛型、DataTable、DataSet数据源,设计的大体思路如下:

一、定义井的实体
public class WellData
{
    public string Name { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
}

二、自定义控件声明属性
1、System.ComponentModel.BindableAttribute
概要:告知属性浏览器绑定到属性是否有意义。带有Bindable(true)标记的属性能显示在从属性浏览器中装载的DataBinding对话框,页面开发者利用它可以把属性和数据绑定表达式关联起来。
应用:属性
样例:[Bindable(true)]
参数类型:Boolean
默认值:false
注释:如果属性没有标记为Bindable(true),页面开发者仍可以通过在页面手动输入表达式来吧属性和一个数据绑定表达式关联起来。

2、System.ComponentModel.BrowsableAtrribute
概要:告知属性浏览器,是否在属性浏览器中显示属性或者事件
应用:属性和事件
样例:[Browsable(false)]
参数类型:Boolean
默认值:true
注释:属性浏览器默认地显示所有的公共属性和事件,仅当想重载默认行为时才使用这个属性。

3、System.ComponentModel.CategoryAttribute
概要:提供了一个分类名,以这个分类名来显示属性或事件。这个属性允许属性浏览器以逻辑分组的形式显示属性和事件。
应用:属性和事件
样例:[Category("WellBinding")]
参数类型:string
默认值:"Default"
注释:这个属性可以被局部化。

4、System.ComponentModel.DefaultEventAttribute
概要:告知属性浏览器哪一个控件事件是默认事件,允许页面开发者双击设计界面中的控件,为默认事件编写事件处理代码。
应用:事件
样例:[DefaultEvent("Click")]
参数类型:string
默认值:无

5、System.ComponentModel.DefaultValueAttribute
概要:为属性提供一个默认值
应用:属性
样例:[DefaultValue(BorderStyle.NoSet)]
参数类型:任何的原始类型、枚举类型或者字符串文字类型。根据属性的标记为复杂属性提供默认值
默认值:无
注释:如果属性的类型具有一个关联的转换器,就可以为复杂属性提供一个默认值。在这种情况下,必须采取属性的双参数形式,形式中第一个参数规定了属性的类型,第二个参数规定了这个值的字符串表示,例如[DefaultValue(typeof(Color),"Red")]

6、System.ComponentModel.DescriptionAttribute
概要:提供一个简单的描述,在用户选择了属性或事件时,属性浏览器就显示这个描述属性和事件
应用:属性和事件
样例:[Description("fieldname wellName")]
参数类型:string
默认值:无
注释:这个属性可以被局部化

7、System.ComponentModel.EditorBrowsableAtrrttribute
概要:告知代码编辑器是否为属性、方法或事件显示对Intellisense的支持属性、方法和事件
应用:属性、方法和事件
样例:[EditorBrowsable(EditorBrowsableState.Never)]
参数类型:EditorBrowsableState枚举型具有一下几种值:
Advanced 仅当开发者想查看高级成员的时候才可以浏览,这种设置用于Microsoft .Net代码编译器
Always 在代码编辑里一直可以浏览
Never 在代码编辑器里一直不可以浏览
默认值:EditorBrowsableState.Always
注释:代码编辑器默认显示对Intellisense的支持。仅当想重载默认行为时才应用这个属性

下面就是我们需要用的属性设置,如下:
private string wellName;
[Browsable(true), Category("WellBinding"), Description("fieldname wellName")]
public string WellName
{
    get { return wellName; }
    set { wellName = value; }
}

private string x;
[Browsable(true), Category("WellBinding"), Description("fieldname x")]
public string X
{
    get { return x; }
    set { x = value; }
}

private string y;
[Browsable(true), Category("WellBinding"), Description("fieldname y")]
public string Y
{
    get { return y; }
    set { y = value; }
}

private object dataSource;//数据源
[AttributeProvider(typeof(IListSource)), Bindable(true), Description("well datasource"), DefaultValue((string)null), Category("Data")]
public object DataSource
{
    get { return dataSource; }
    set
    {
        if (value != null)
        {
            if (value is DataTable)
            {
                dataSource = new DataView(dataSource as DataTable);
            }
            else if (dataSource is IList)
            {
                dataSource = dataSource as IList;
            }
            else
                dataSource = dataSource as IEnumerable;
        }
        dataSource = value;
    }
}

三、数据源的解析,绘图部分略
IList<WellData> wellLists = new List<WellData>();
foreach (var item in dataSource)
{
    WellData well = new WellData();
    object Name =Helper.ConvertHerlper.ConvertEnumerationItem(item, this.wellName);
    object X = Helper.ConvertHerlper.ConvertEnumerationItem(item, this.x);
    object Y = Helper.ConvertHerlper.ConvertEnumerationItem(item, this.y);

    well.Name = Name.ToString();
    well.X = Convert.ToDouble(X);
    well.Y = Convert.ToDouble(Y);
    wellLists.Add(well);
}

四、将IList泛型、DataTable、DataSet、IEnumerable类型元素转换为组件需要的实体
public static object ConvertEnumerationItem(object item, string fieldName)
{
    DataRow row = item as DataRow;
    if (row != null)
    {
        if (!string.IsNullOrEmpty(fieldName))
        {
            if (row.Table.Columns.Contains(fieldName))
                return row[fieldName];
        }
        return row[0];
    }
    else
    {
        PropertyDescriptor descriptor = TypeDescriptor.GetProperties(item).Find(fieldName, true);
        if (descriptor != null)
            return (descriptor.GetValue(item) ?? null);
    }
    return null;
}

五、客户端应用
自定义控件.WellName="代表客户端实体字段1";
自定义控件.X="代表客户端实体字段2";
自定义控件.Y="代表客户端实体字段3";
自定义控件.DataSource = IList<客户端实体>;//或者DataTable、DataSet数据源

原创粉丝点击