商信互联
以下示例说明了如何在XPO持久性类中实现集合属性。
不同的集合属性在UI中具有不同的操作。您可以在模型编辑器中管理New,Delete,Link或Unlink Action的可见性。列表视图的设置AllowNew,AllowDelete,AllowLink,或AllowUnlink属性为假隐藏这些操作。
请参考FeatureCenter演示以查看有关如何实现集合属性的示例(请参见%PUBLIC%\ Documents \ DevExpress演示19.2 \ Components \ eXpressApp Framework \ FeatureCenter \ CS \ FeatureCenter.Module \ PropertyEditors \ CollectionPropertiesObject.cs)。
[Association("CollectionProperties-AssociatedObject")]
public XPCollection<AssociatedObject> Association {
get { return GetCollection<AssociatedObject>(nameof(Association)); }
}
<Association("CollectionProperties-AssociatedObject")>
Public ReadOnly Property Association() As XPCollection(Of AssociatedObject)
Get
Return GetCollection(Of AssociatedObject)(NameOf(Association))
End Get
End Property
有关XPO中关联集合的更多信息,请参见“代码与UI中的持久对象之间的关系”。
[Association("CollectionProperties-AggregatedObject"), Aggregated]
public XPCollection<AggregatedObject> AggregatedAssociation {
get { return GetCollection<AggregatedObject>(nameof(AggregatedAssociation)); }
}
<Association("CollectionProperties-AggregatedObject"), Aggregated>
Public ReadOnly Property AggregatedAssociation() As XPCollection(Of AggregatedObject)
Get
Return GetCollection(Of AggregatedObject)(NameOf(AggregatedAssociation))
End Get
End Property
有关XPO中聚合集合的更多信息,请参见“代码和UI中的持久对象之间的关系”。
如果集合不是关联的一部分,则使用对象填充此集合,并实施用于管理它们的自定义逻辑。要在UI中隐藏或显示“新建”和“链接”或“删除和取消链接”操作,请使用CollectionOperationSet属性装饰collection属性,并指定AllowAdd和AllowRemove参数。
在新的,删除,链接和取消链接操作都可以在此集合。我们建议使用此集合来显示对象,而不要在UI中对其进行编辑。在下面的代码片段中,CollectionOperationSet的AllowAdd和AllowRemove参数设置为false以拒绝用户更改UI中的集合:
private XPCollection<NoAssociationObject> noAssociation;
[CollectionOperationSet(AllowAdd = false, AllowRemove = false)]
public XPCollection<NoAssociationObject> NoAssociation {
get {
if(noAssociation == null) {
noAssociation = new XPCollection<NoAssociationObject>(Session);
}
return noAssociation;
}
}
Private _noAssociation As XPCollection(Of NoAssociationObject)
<CollectionOperationSet(AllowAdd := False, AllowRemove := False)>
Public ReadOnly Property NoAssociation() As XPCollection(Of NoAssociationObject)
Get
If _noAssociation Is Nothing Then
_noAssociation = New XPCollection(Of NoAssociationObject)(Session)
End If
Return _noAssociation
End Get
End Property
您可以使用Criteria属性来过滤集合对象,或者将LoadingEnabled属性设置为false并将对象添加到集合中。订阅集合的CollectionChanged事件,以实现New,Delete,Link和Unlink Actions所需的逻辑。
在新的,删除,链接和取消链接操作都可以在此集合。
private BindingList<NoAssociationObject> persistentBindingList;
public BindingList<NoAssociationObject> PersistentBindingList {
get {
if(persistentBindingList == null) {
persistentBindingList = new BindingList<NoAssociationObject>();
foreach(NoAssociationObject obj in NoAssociation) {
persistentBindingList.Add(obj);
}
}
return persistentBindingList;
}
}
Private _PersistentBindingList As BindingList(Of NoAssociationObject)
Public ReadOnly Property PersistentBindingList As BindingList(Of NoAssociationObject)
Get
If _PersistentBindingList Is Nothing Then
_PersistentBindingList = New BindingList(Of NoAssociationObject)()
For Each obj As NoAssociationObject In NoAssociation
_PersistentBindingList.Add(obj)
Next
End If
Return _PersistentBindingList
End Get
End Property
订阅集合的ListChanged事件,以实现New,Delete,Link和Unlink Actions所需的逻辑。
在新的,删除,链接和取消链接,如果它的主人对象是一个持久化对象操作不适用于这个集合。如果主对象是非持久对象,则“新建”和“删除操作”可用。要激活链接和取消链接,请将LinkUnlinkController.RequirePersistentType属性设置为false并处理CustomCreateLinkView事件。
private BindingList<SomeObject> nonPersistentObjectBindingList;
private void EnsureNonPersistentObjectBindingList() {
if(nonPersistentObjectBindingList == null) {
nonPersistentObjectBindingList = new BindingList<SomeObject>();
// ...
}
}
public BindingList<SomeObject> NonPersistentBindingList {
get {
EnsureNonPersistentObjectBindingList();
return nonPersistentObjectBindingList;
}
}
Private nonPersistentObjectBindingList As BindingList(Of SomeObject)
Private Sub EnsureNonPersistentObjectBindingList()
If nonPersistentObjectBindingList Is Nothing Then
nonPersistentObjectBindingList = New BindingList(Of SomeObject)()
' ...
End If
End Sub
Public ReadOnly Property NonPersistentBindingList As BindingList(Of SomeObject)
Get
EnsureNonPersistentObjectBindingList()
Return nonPersistentObjectBindingList
End Get
End Property