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
FilteringCriterion
business 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 24.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
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 additionalType
property that specifies the target object type. In this sample, it is theObjectType
property. Additionally, an EditorAliasAttribute with thePopupCriteriaPropertyEditor
alias is applied to theCriterion
property. This attribute changes the default Property Editor for the property:PopupFilterPropertyEditor
for ASP.NET Core Blazor applicationsPopupCriteriaPropertyEditor
for Windows Forms applicationsASPxPopupCriteriaPropertyEditor
for ASP.NET Web Forms applications
Optional. If you use Entity Framework Core, register the
FilteringCriterion
type 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(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
Add the following
CriteriaController
class 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
FilterCriterion
objects whoseObjectType
matches 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
Criterion
property’s value of aFilterCriterion
object to theDevExpress.Data.Filtering.CriteriaOperator
object, you need to call the staticCriteriaEditorHelper.GetCriteriaOperator
method. This method takes three parameters. Thestring
parameter is the criterion to be converted. TheType
parameter specifies the object type for which the criterion is constructed. TheObjectSpace
parameter 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