How to: Display a List of Non-Persistent Objects in a Popup Dialog(如何:在弹出对话框中显示非持久性对象列表)
This example demonstrates how to populate and display a list of objects that are not bound to the database (non-persistent objects). A sample application that stores a list of books will be created in this example. An Action that displays a list of duplicate books in a pop-up window will be added to this application.
此示例演示如何填充和显示未绑定到数据库的对象列表(非持久性对象)。此示例中将创建一个存储图书列表的示例应用程序。将向此应用程序添加一个在弹出窗口中显示重复图书列表的操作。
Tip
A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e980/how-to-display-a-list-of-non-persistent-objects.
https://supportcenter.devexpress.com/ticket/details/e980/how-to-display-a-list-of-non-persistent-objects的DevExpress代码示例数据库中提供了完整的示例项目。
1.Define the Book persistent class.
定义Book持久类。
C#
[DefaultClassOptions]
public class Book : BaseObject {
public Book(Session session) : base(session) { }
public string Title {
get { return GetPropertyValue<string>(nameof(Title)); }
set { SetPropertyValue<string>(nameof(Title), value); }
}
}
The Book class, as its name implies, is used to represent books. It inherits BaseObject, and thus it is persistent. It contains a single Title property, which stores a book’s title.
顾名思义,Book类用于表示书籍。它继承了BaseObject,因此它是持久的。它包含一个标题属性,该属性存储一本书的标题。
2.Define two non-persistent classes - Duplicate and DuplicatesList.
定义两个非持久性类——Duplate和DuplicatesList。
C#
[DomainComponent]
public class Duplicate {
[Browsable(false), DevExpress.ExpressApp.Data.Key]
public int Oid;
public string Title { get; set; }
public int Count { get; set; }
}
[DomainComponent]
public class DuplicatesList {
private BindingList<Duplicate> duplicates;
public DuplicatesList() {
duplicates = new BindingList<Duplicate>();
}
public BindingList<Duplicate> Duplicates { get { return duplicates; } }
}
Note
The INotifyPropertyChanged, IXafEntityObject and IObjectSpaceLink interface implementations were omitted in this example. However, it is recommended to support these interfaces in real-world applications (see The Importance of Property Change Notifications for Automatic UI Updates and Non-Persistent Objects).
本示例中省略了INotifyPropertyChanged、IXafEntityObject和IObjectSpaceLink接口实现。但是,建议在实际应用程序中支持这些接口(请参阅自动UI更新和非持久性对象的属性更改通知的重要性)。
These classes are not inherited from an XPO base persistent class, and are not added to the Entity Framework Core data model. As a result, they are not persistent. Use the DomainComponentAttribute to add such classes to the Application Model. This affects application UI construction.
这些类不是从XPO基持久类继承的,也没有添加到Entity Framework Core数据模型中,因此它们不是持久的。使用DomainComponentAttribute将此类类添加到应用程序模型中。这会影响应用程序UI构造。
The Duplicate non-persistent class has three public properties - Title, Count, and Id. The Title property stores the title of a book. The Count property stores the total number of books with the title specified by the Title property. Id is a key property. A key property is required in a non-persistent class for correct ListView operation. To specify a key property, use the KeyAttribute. The DuplicatesList non-persistent class aggregates the duplicates via the Duplicates collection property of the BindingList<Duplicate> type.
重复非持久类有三个公共属性——标题、计数和ID。标题属性存储图书的标题。计数属性存储标题由标题属性指定的图书总数。ID是一个键属性。非持久类中需要一个键属性才能正确执行ListView操作。要指定键属性,请使用密钥属性。重复列表非持久类通过BindingList<Duplicate>类型的重复集合属性聚合重复项。
3.Create the following ShowDuplicateBooksController View Controller.
创建以下ShowDuplicateBooksController视图控制器。
C#
public class ShowDuplicateBooksController : ObjectViewController<ListView, Book> {
public ShowDuplicateBooksController() {
PopupWindowShowAction showDuplicatesAction =
new PopupWindowShowAction(this, "ShowDuplicateBooks", "View");
showDuplicatesAction.CustomizePopupWindowParams += showDuplicatesAction_CustomizePopupWindowParams;
}
void showDuplicatesAction_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
Dictionary<string, int> dictionary = new Dictionary<string, int>();
foreach(Book book in View.CollectionSource.List) {
if(!string.IsNullOrEmpty(book.Title)) {
if(dictionary.ContainsKey(book.Title)) {
dictionary[book.Title]++;
}
else
dictionary.Add(book.Title, 1);
}
}
var nonPersistentOS = Application.CreateObjectSpace(typeof(DuplicatesList));
DuplicatesList duplicateList =nonPersistentOS.CreateObject<DuplicatesList>();
int duplicateId = 0;
foreach(KeyValuePair<string, int> record in dictionary) {
if (record.Value > 1) {
var dup = nonPersistentOS.CreateObject<Duplicate>();
dup.Id = duplicateId;
dup.Title = record.Key;
dup.Count = record.Value;
duplicateList.Duplicates.Add(dup);
duplicateId++;
}
}
nonPersistentOS.CommitChanges();
DetailView detailView = Application.CreateDetailView(nonPersistentOS, duplicateList);
detailView.ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.Edit;
e.View = detailView;
e.DialogController.SaveOnAccept = false;
e.DialogController.CancelAction.Active["NothingToCancel"] = false;
}
}
This Controller inherits ObjectViewController<ListView, Book>, and thus, targets List Views that display Books. In the Controller’s constructor, the ShowDuplicateBooks PopupWindowShowAction is created. In the Action’s PopupWindowShowAction.CustomizePopupWindowParams event handler, the Books are iterated to find the duplicates. For each duplicate book found, the Duplicate object is instantiated and added to the DuplicatesList.Duplicates collection. Note that a unique key value (Id) is assigned for each Duplicate. Finally, a DuplicatesList Detail View is created and passed to the handler’s CustomizePopupWindowParamsEventArgs.View parameter. As a result, a DuplicatesList object is displayed in a pop-up window when a user clicks ShowDuplicateBooks.
此控制器继承ObjectViewController<ListView, Book>,因此以显示图书的列表视图为目标。在控制器的构造函数中,创建了ShowDuplicateBooksPopupWindowShowAction。在Action的PopupWindowShowActionCustomizePopupWindowParams事件处理程序中,迭代图书以查找重复项。对于找到的每一本重复图书,都会实例化重复对象并将其添加到DuplicatesList.Duplicates集合中。请注意,为每个重复分配了一个唯一的键值(Id)。最后,创建一个DuplicatesList详细信息视图并将其传递给处理程序的CustomizePopupWindowParamsEventArgs视图参数。因此,当用户单击ShowDuplicateBooks时,会在弹出窗口中显示一个DuplicatesList对象。
The following images illustrate the implemented Action and its pop-up window.
下图说明了实现的Action及其弹出窗口。
Windows Forms
ASP.NET Web Forms
Blazor