随笔 - 76, 文章 - 3, 评论 - 34, 引用 - 0
数据加载中……

我来说说自定义对象集合的绑定

我只是在这里抛砖引玉,这其中好多结果都是参照其他技术人员的一些例子和介绍,不过我始终没有找到全面的介绍(就是事无具细的)不过这样也好,所以只能自己摸索,这其中我也有好多不解之处.

我就按我如何来解决这些问题来写写.

我的自定义对象集合是是继承于CollectionBase的.因为CollectionBase已经实现了ILIST接口,也就是说可能直接绑定到了DataGrid控件.但是在默认情况自定义类的公共属性都会绑定到datagrid.这显然是不符合要求的.
按我们实际使用情况如下:
1.可以指定要绑定的属性.
2.可以控制绑定属性的列位置.
这时通过查msdn发现了接口ItypedList好像可以实现这些要求.通过google也找到一个关于绑定的例子.如何实现itypedlist接口.最重要的接口方法是GetItemProperties,他就能控制你自定义的那些属性可以进行绑定.返回的值是PropertyDescriptorCollection集合,所以我们需要实现一个自定义属性描述的类CustomPropertyDescriptor它的基类是System.ComponentModel.PropertyDescriptor.重写其抽象方法.其中要注意的是IsReadOnly属性,这个属性就可以控制在datagrid控件中是否允许编辑此属性.
CustomPropertyDescriptor的实现

 Public Class CustomPropertyDescriptor
        
Inherits System.ComponentModel.PropertyDescriptor


        
Private md_name As String = String.Empty
        
Private md_pi As System.Reflection.PropertyInfo
        
Private md_attribute As FieldAttribute

        
Public Sub New(ByVal name As StringByVal pi As System.Reflection.PropertyInfo)
         
            
MyBase.New(name, pi.GetCustomAttributes(GetType(Attribute), True))
            
Me.md_name = name
            
Me.md_pi = pi

        
End Sub


        
Public Overrides Function GetValue(ByVal component As ObjectAs Object
            
'Debug.WriteLine("GetValue")
            Return CType(component, EntityBase).GetValue(Me.md_pi.Name)
        
End Function


        
Public Overrides ReadOnly Property ComponentType() As System.Type
            
Get
                
Return Me.md_pi.DeclaringType
            
End Get
        
End Property


        
Public Overrides ReadOnly Property PropertyType() As System.Type
            
Get
                
Return Me.md_pi.PropertyType
            
End Get
        
End Property


        
Public Overrides Sub ResetValue(ByVal component As Object)

        
End Sub


        
Public Overrides Sub SetValue(ByVal component As ObjectByVal value As Object)
            
'Debug.WriteLine("SetValue")
            CType(component, EntityBase).SetValue(Me.md_pi.Name, value)
        
End Sub


        
Public Overrides Function CanResetValue(ByVal component As ObjectAs Boolean
            
Return False
        
End Function


        
Public Overrides Function ShouldSerializeValue(ByVal component As ObjectAs Boolean
            
Return False
        
End Function


        
Public Overrides ReadOnly Property IsReadOnly() As Boolean
            
Get
                
Return Not Me.md_pi.CanWrite
            
End Get
        
End Property


    
End Class

以下强类型集合的实现
    Public Class EntityContainer
        Inherits CollectionBase
        Implements ITypedList, IBindingList
        

ItypeList接口

以前就是我的ItypedList接口的实现其中属性ItemType是指定我当前集合中存放的实体类型.我已定义的实体具有方法GetCustomProperties就是用于返回我当前实体那些属性是允许进行绑定的.

IBindingList接口实现

实现了ItypedList接口,虽然我们绑定到datagrid已实现了我们的要求,但是这时,不能编辑,不能添加,不能删除,如果要实现这些功能,我们就需要实现IBindingList接口.这个接口主要是用于是否允许编辑,是否允许移除,排序,查找等,排序和查找我没有实现.

这时我以为现在可以正常的完成了绑定,不过事情还没有完,我在进行编辑的时候,有时会有异常产生,在通过google查询绑定方面的资料时,对集合的项目进行改变时要引ListChanged事件.
所以需要重写CollectionBase的一些方法