的BindingList< T>当< T>时,不会设置ListChangedEventArgs的PropertyDescriptor属性是ICusomTypeDescriptor。 [英] BindingList<T> doesn't set PropertyDescriptor property of ListChangedEventArgs when <T> is an ICusomTypeDescriptor.

查看:80
本文介绍了的BindingList< T>当< T>时,不会设置ListChangedEventArgs的PropertyDescriptor属性是ICusomTypeDescriptor。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎缺乏对BindingList< T>的支持。对于< T>时的ListChanged事件实现ICustomTypeDescriptor。 ListChhanged事件在BindingList上被适当地触发以响应其< T>对象触发INotifyPropertyChanged.PropertyChanged事件。但是,由BindingList创建的ListChangedEventArgs不会用任何东西填充PropertyDescriptor属性(以指示哪个属性导致列表更改事件触发)。

There appears to be lack of support on BindingList<T> for ListChanged events when <T> implements ICustomTypeDescriptor. The ListChanged event is properly fired on the BindingList in response to its <T> objects firing INotifyPropertyChanged.PropertyChanged events. However, the ListChangedEventArgs created by the BindingList does not fill the PropertyDescriptor property with anything (to indicate which property caused the list changed event to fire).

为了澄清,问题发生在以下场景:

Just to clarify, the problem occurs in the following scenario:


  • 创建了一个实现ICustomTypeDescriptor和INotifyPropertyChanged的业务对象。我已经实现了ICustomTypeDescriptor.GetProperties()方法来为我的对象发出自定义PropertyDescriptors。为了简化示例,我将自定义(继承)PropertyDescriptors作为对象上的内部数据成员。

  • 创建了一个BindingList< MyBusinessObject>并设置SupportsChangeNotifications = True。

  • 在BindingList上为ListChanged事件添加了处理程序

  • 更改了业务对象上属性的值,它反过来触发一个PropertyChanged事件,该事件由BindingList处理(因为BindingLists知道INotifyPropertyChanged子对象)。我的自定义PropertyDescriptor的'Name'被传递到PropertyChangedEventArgs的构造函数中,这是由INotifyPropertyChanged.PropertyChanged事件传递的EventArgs对象。

  • BindingList的ListChanged事件的处理程序被调用。

  • 在检查ListChangedEventArgs时,我发现1)ListChangeType = ListChanged(这是正确的和预期的),但是PropertyDescriptor属性是Null(这是不正确的!,它应该是我传递给我的PropertyChangedEventArgs的'Name'对应的PropertyDescriptor!)

以下是一些示例代码snipets:

Here's some example code snipets:

< font face ="Courier New,Courier,Monospace">'这是我的自定义属性描述符类的简化版本
Public Class MyCustomPropertyDescriptor
Inherits PropertyDescriptor

Sub New(propName as String)
MyBase.New(propName)
End Sub

'这里没什么特别的......

结束类


'我的simplfied业务对象类
公共类MyBusinessObject
实现ICustomPropertyDescriptor,INotifyPropertyChanged

'B.O属性之一的属性描述符实例。
'将其作为实例字段仅用于简化示例
private _emailAddressPropDesc as MyCustomPropertyDescriptor
private _emailAddress As String = string.Empty

公共事件PropertyChanged As PropertyChangedEventHander _
实现INotifyPropertyChanged.PropertyChanged

Sub New()

_emailAddressPropDesc = new MyCustomPopertyDescriptor(" email&quo t;)

End Sub

...........

'这里是我的自定义属性描述符的发现方式>'外部组件。 GetProperties(attributes())重载
'类似地实现。
Public Function GetProperties()As PropertyDescriptorCollection _
实现ICustomTypeDescriptor.GetProperties

< font face ="Courier New"> Dim descriptors()As PropertyDescriptor = {_emailAddressPropDesc}

返回新的PropertyDescriptorCollection(描述符)

结束函数

.. .........

'这是一个触发PropertyChanged事件的示例属性。
'再次,简化用于解释目的
公共属性EmailAddress As String
Get
return _emailAddress
End Get
Set(value as String)
_emailAddress = value
RaiseEvent PropertyChanged(_emailAddressPropDesc.Name)
End Set
End Property


............

End Class

'和这里是一个表单中的一些测试代码,其中创建了绑定列表

并且处理了ListChanged事件
Sub Form_Load(...)

Dim list As New BindingList(MyBusinessObject)
Dim businessObject As New MyBusinessObject(...)

"设置此项以确保触发ListChanged事件
list.SupportsChangeNotifications = True

'添加列表已更改处理程序
AddHandler list.ListChanged,AddressOf List_ListChanged

'将业务对象添加到列表中,这将导致ListChanged
'[ItemAdded]事件(这里没问题)
list.Add( businessObject)

'现在我要修改我的业务对象上的一个值,该值将触发一个PropertyChanged事件,该事件由
'BindingList内部处理(因为它知道INotifyPropertyChanged对象)
'并将导致BindingList.ListChanged [ItemChanged]事件
'触发(到目前为止)。但是'ListChangedEventArgs的PropertyDescriptor属性应该设置为_emailAddressPropDesc,
'但它不是,为什么?

businessObject.EmailAddress = < a title ="mailto:why@doesntThisWork.com"href ="mailto:why@doesntThisWork.com">" why@doesntThisWork.com "

End Sub

'BindingList.ListChanged事件处理程序
Sub List_ListChanged(发件人作为对象,e作为ListChangedEventArgs)

'所以这里我们设置了我的BO的EmailAddress属性,
'e.ListChangedType = ListChanged(ok)但是'e.PropertyDescriptor Is Nothing(不行,它,它)应该是'_emailAddressPropDesc

End Sub

所以我很确定问题是BindingList< T>的内部问题。似乎无法根据属性描述符名称找到我的自定义属性描述符,它从其处理程序接收到INotifyPropertyChange.PropertyChanged事件。参与MS的绑定组件开发的人是否可以看到这一点,因为这个小问题正在使BindingList< T>对我来说相当无用,否则它可能是一个非常有用的类?

谢谢,
Nate A

Thanks,
Nate A

推荐答案

看一下我的文章:

Take a look at my article:

http://www.codeproject.com/KB/WPF/notifyparent.aspx

 

 

 


这篇关于的BindingList&LT; T&GT;当&lt; T&gt;时,不会设置ListChangedEventArgs的PropertyDescriptor属性是ICusomTypeDescriptor。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆