How to: Enable Database Storage for User-Defined Criteria that Filter a List View(如何:为筛选列表视图的用户定义条件启用数据库存储)
This topic demonstrates how to design and save filtering criteria at runtime in an XAF application. The scenario consists of two steps:
本主题演示如何在 XAF 应用程序中运行时设计并保存筛选条件。该场景包含两个步骤:
- Define the FilteringCriterion business class that implements filtering criteria for a List View.(定义实现列表视图筛选条件的FilteringCriterion业务类。)
- Create a custom View Controller that contains an Action used to filter the List View.(创建一个自定义视图控制器,其中包含一个用于过滤列表视图的操作。)
Note
To follow the instructions in this topic, you can use the MainDemo.NET application installed with the XAF package. The default location of the application is %PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF.
要按照本主题中的说明进行操作,你可以使用随XAF包安装的MainDemo.NET应用程序。该应用程序的默认位置是%PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF。
Step 1 - Define the FilteringCriterion Business Class(步骤1 - 定义筛选标准业务类)
1.Add a class to the BusinessObjects folder of the MainDemo.Module project and name it FilteringCriterion.cs. Replace auto-generated code with the following class declaration:
在MainDemo.Module项目的BusinessObjects文件夹中添加一个类,并将其命名为FilteringCriterion.cs。用以下类声明替换自动生成的代码:
C# (EF Core)
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Editors;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
namespace MainDemo.Module.BusinessObjects {
[DefaultClassOptions, ImageName("Action_Filter")]
public class FilteringCriterion : BaseObject {
private Type objectType;
public virtual string Description { get; set; }
[Browsable(false)]
public virtual string ObjectTypeName {
get { return objectType == null ? string.Empty : objectType.FullName; }
set {
ITypeInfo typeInfo = XafTypesInfo.Instance.FindTypeInfo(value);
objectType = typeInfo == null ? null : typeInfo.Type;
}
}
[NotMapped, ImmediatePostData]
public Type ObjectType {
get { return objectType; }
set {
if(objectType == value)
return;
objectType = value;
Criterion = string.Empty;
}
}
[CriteriaOptions(nameof(ObjectType))]
[FieldSize(FieldSizeAttribute.Unlimited)]
[EditorAlias(EditorAliases.PopupCriteriaPropertyEditor)]
public virtual string Criterion { get; set; }
}
}
C# (XPO)
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Editors;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
namespace MainDemo.Module.BusinessObjects {
[DefaultClassOptions, ImageName("Action_Filter")]
public class FilteringCriterion : BaseObject {
private Type objectType;
public virtual string Description { get; set; }
[Browsable(false)]
public virtual string ObjectTypeName {
get { return objectType == null ? string.Empty : objectType.FullName; }
set {
ITypeInfo typeInfo = XafTypesInfo.Instance.FindTypeInfo(value);
objectType = typeInfo == null ? null : typeInfo.Type;
}
}
[NotMapped, ImmediatePostData]
public Type ObjectType {
get { return objectType; }
set {
if(objectType == value)
return;
objectType = value;
Criterion = string.Empty;
}
}
[CriteriaOptions(nameof(ObjectType))]
[FieldSize(FieldSizeAttribute.Unlimited)]
[EditorAlias(EditorAliases.PopupCriteriaPropertyEditor)]
public virtual string Criterion { get; set; }
}
}
Objects of this class implement filtering criteria.
此类的对象实现过滤条件。
The Description field contains the text that describes a criterion. These descriptions are used to fill a drop-down list of possible filters.
“描述”字段包含描述某个标准的文本。这些描述用于填充可能的筛选器下拉列表。
The Criterion property contains a criterion. To use the built-in XAF Criteria Property Editors, the property that contains a criterion must have a CriteriaOptionsAttribute. The attribute’s parameter is the name of an additional Type property that specifies the target object type. In this sample, it is the ObjectType property. Additionally, an EditorAliasAttribute with the PopupCriteriaPropertyEditor alias is applied to the Criterion property. This attribute changes the default Property Editor for the property:
Criterion属性包含一个条件。要使用内置的XAF条件属性编辑器,包含条件的属性必须具有CriteriaOptionsAttribute。该特性的参数是一个附加的Type属性的名称,该属性指定目标对象类型。在本示例中,它是ObjectType属性。此外,将带有PopupCriteriaPropertyEditor别名的EditorAliasAttribute应用于Criterion属性。此特性会更改该属性的默认属性编辑器:
- PopupFilterPropertyEditor for ASP.NET Core Blazor applications(适用于ASP.NET Core Blazor应用程序的弹出筛选属性编辑器)
- PopupCriteriaPropertyEditor for Windows Forms applications(适用于Windows窗体应用程序的弹出式条件属性编辑器)
- ASPxPopupCriteriaPropertyEditor for ASP.NET Web Forms applications(适用于ASP.NET Web Forms应用程序的ASPxPopupCriteriaPropertyEditor)
2.Optional. If you use Entity Framework Core, register the FilteringCriterion type in DbContext:
可选。如果使用Entity Framework Core,请在DbContext中注册FilteringCriterion类型:
C#
using DevExpress.ExpressApp.Design;
using DevExpress.ExpressApp.EFCore.DesignTime;
using DevExpress.Persistent.Base;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace MainDemo.Module.BusinessObjects;
//..
[TypesInfoInitializer(typeof(MainDemoDbContextContextInitializer))]
public class MainDemoDbContext : DbContext {
public MainDemoDbContext(DbContextOptions<MainDemoDbContext> options) : base(options) {
}
public DbSet<FilteringCriterion> FilteringCriteria { get; set; }
// ...
}
Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
添加一个迁移并更新数据库。详情见以下部分:使用数据库管理系统:设置迁移。
Step 2 - Create a Custom List View Controller(步骤2 - 创建自定义列表视图控制器)
1.Add the following CriteriaController class to the Controllers folder of the MainDemo.Module project:
将以下CriteriaController类添加到MainDemo.Module项目的Controllers文件夹中:
C#
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using MainDemo.Module.BusinessObjects;
namespace MainDemo.Module.Controllers {
public class CriteriaController : ViewController<ListView> {
private SingleChoiceAction filteringCriterionAction;
public CriteriaController() {
filteringCriterionAction = new SingleChoiceAction(
this, "FilteringCriterion", PredefinedCategory.Filters);
filteringCriterionAction.Execute += FilteringCriterionAction_Execute;
}
protected override void OnActivated() {
filteringCriterionAction.Items.Clear();
foreach(FilteringCriterion criterion in ObjectSpace.GetObjects<FilteringCriterion>()) {
if(criterion.ObjectType.IsAssignableFrom(View.ObjectTypeInfo.Type)) {
filteringCriterionAction.Items.Add(new ChoiceActionItem(criterion.Description, criterion.Criterion));
}
}
if(filteringCriterionAction.Items.Count > 0) {
filteringCriterionAction.Items.Add(new ChoiceActionItem("All", null));
}
}
private void FilteringCriterionAction_Execute(object sender, SingleChoiceActionExecuteEventArgs e) {
var collectionSource = View.CollectionSource;
collectionSource.BeginUpdateCriteria();
collectionSource.Criteria.Clear();
collectionSource.Criteria[e.SelectedChoiceActionItem.Caption] = ObjectSpace.ParseCriteria(e.SelectedChoiceActionItem.Data as string);
collectionSource.EndUpdateCriteria();
}
}
}
This Controller adds the FilteringCriterion SingleChoiceAction and populates the Action’s dropdown list with all existing FilterCriterion objects whose ObjectType matches the type of objects displayed in the current List View.
此控制器添加筛选条件单选操作,并使用所有现有筛选条件对象填充该操作的下拉列表,这些对象的对象类型与当前列表视图中显示的对象类型相匹配。
The FilteringCriterion Action applies the criterion selected in the dropdown list to the List View. To convert the Criterion property’s value of a FilterCriterion object to the DevExpress.Data.Filtering.CriteriaOperator object, you need to call the static CriteriaEditorHelper.GetCriteriaOperator method. This method takes three parameters. The string parameter is the criterion to be converted. The Type parameter specifies the object type for which the criterion is constructed. The ObjectSpace parameter specifies any IObjectSpace that contains objects of the specified type.
“筛选条件”操作将在下拉列表中选择的条件应用于列表视图。若要将“筛选条件”对象的“条件”属性值转换为DevExpress.Data.Filtering.CriteriaOperator对象,需要调用静态方法CriteriaEditorHelper.GetCriteriaOperator。此方法接受三个参数。字符串参数是要转换的条件。Type参数指定为其构造条件的对象类型。ObjectSpace参数指定包含指定类型对象的任何IObjectSpace。
Note
You should not use the CriteriaOperator.Parse method in this scenario. Criteria Property Editors can generate Criteria Strings containing Object Parameters, which are specific to XAF and cannot be parsed by CriteriaOperator.Parse.
在这种情况下,您不应使用CriteriaOperator.Parse方法。标准属性编辑器可以生成包含对象参数的标准字符串,这些参数特定于XAF,无法由CriteriaOperator.Parse进行解析。
2.Run the application, create several business objects, and then try the FilteringCriterion action.
运行该应用程序,创建几个业务对象,然后尝试“过滤条件”操作。
ASP.NET Core Blazor
Windows Forms
You can also review Criteria Property Editors in the Property Editors | Criteria Properties section in the Feature Center demo installed with XAF. The default location of the application is %PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF\FeatureCenter.NETFramework.XPO.
你还可以在与 XAF 一同安装的功能中心演示的“属性编辑器 | 标准属性”部分中查看标准属性编辑器。该应用程序的默认位置是 %PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF\FeatureCenter.NETFramework.XPO。