How to: Enable Database Storage for User-Defined Criteria that Filter a List View
- 5 minutes to read
This topic demonstrates how to design and save filter criteria at runtime in an XAF application. The scenario consists of two steps:
- Define the
FilteringCriterionbusiness class that implements filter criteria for a List View. - 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 25.1\Components\XAF. You can also download the full implementation from the following GitHub page: How to use Criteria Property Editors.
Step 1 - Define the FilteringCriterion Business Class
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:
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
Descriptionfield contains the text that describes a criterion. These descriptions are used to fill a drop-down list of possible filters.The
Criterionproperty 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 additionalTypeproperty that specifies the target object type. In this sample, it is theObjectTypeproperty. Additionally, an EditorAliasAttribute with thePopupCriteriaPropertyEditoralias is applied to theCriterionproperty. This attribute changes the default Property Editor for the property:PopupFilterPropertyEditorfor ASP.NET Core Blazor applicationsPopupCriteriaPropertyEditorfor Windows Forms applicationsASPxPopupCriteriaPropertyEditorfor ASP.NET Web Forms applications
Optional. If you use Entity Framework Core, register the
FilteringCriteriontype inDbContext: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(DbContextTypesInfoInitializer<MainDemoDbContext>))] public class MainDemoDbContext : DbContext { public MainDemoDbContext(DbContextOptions<MainDemoDbContext> options) : base(options) { } public DbSet<FilteringCriterion> FilteringCriteria { get; set; } // ... }Note
Unlike native EF Core, XAF automatically updates database schema when the structure of business objects changes. However, you can use EF Core migrations to update database schema manually. Refer to the following help topic for additional information: Update Database Schema and Migrations in EF Core.
Step 2 - Create a Custom List View Controller
Add the following
CriteriaControllerclass to the Controllers folder of the MainDemo.Module project: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 drop-down list with all existing
FilterCriterionobjects whoseObjectTypematches the type of objects displayed in the current List View.The FilteringCriterion Action applies the criterion selected in the drop-down list to the List View. To convert the
Criterionproperty’s value of aFilterCriterionobject to theDevExpress.Data.Filtering.CriteriaOperatorobject, you need to call the staticCriteriaEditorHelper.GetCriteriaOperatormethod. This method takes three parameters. Thestringparameter is the criterion to be converted. TheTypeparameter specifies the object type for which the criterion is constructed. TheObjectSpaceparameter specifies any IObjectSpace that contains objects of the specified type.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.Run the application, create several business objects, and then try the FilteringCriterion action.
- ASP.NET Core Blazor

- Windows Forms
