This topic demonstrates options for creating criteria when you use any of the filtering techniques in an XAF application.
A Criteria as a CriteriaOperator Object
When you are required to set a criteria as a CriteriaOperator object, you have the following options:
Use Criteria Operators provided by XPO. There are several XPO criteria operators that you can use to build the required criteria. The following code demonstrates how to build a simple criteria using the BinaryOperator:
// The criteria that represents a logical expression (City <> "Chicago")
// is represented by the BinaryOperator and two operands.
CriteriaOperator criteria = new BinaryOperator(
new OperandProperty("City"), new OperandValue("Chicago"),
' The criteria that represents a logical expression (City <> "Chicago")
' is represented by the BinaryOperator and two operands.
Dim criteria As CriteriaOperator = New BinaryOperator( _
New OperandProperty("City"), New OperandValue("Chicago"), _
Use the CriteriaOperator.Parse method. You can represent criteria as a human-readable string and parse it using the static CriteriaOperator.Parse method.
//The criteria that represents a logical expression (City <> "Chicago") is represented by a string.
CriteriaOperator criteria = CriteriaOperator.Parse("City != 'Chicago'");
'The criteria that represents a logical expression (City <> "Chicago") is represented by a string.
Dim criteria As CriteriaOperator = CriteriaOperator.Parse("City != 'Chicago'")
Anywhere you are required to specify a criteria as a string, you should set a string that can be parsed by the static CriteriaOperator.Parse method. To learn the syntax used in this method, refer to the Criteria Language Syntax help topic.
The places where you should specify a criteria as a string include properties in the Model Editor, and attributes and different properties and methods in code.
The following code demonstrates how to set criteria for the ActionBase.TargetObjectsCriteria property of an Action. The Controller below makes the Action available when the selected Task's DueDate property value is less than or equal to the system's date and time:
Public Partial Class MyController
Private Sub MyController_AfterConstruction(ByVal sender As Object, ByVal e As EventArgs)
TargetObjectType = GetType(Task)
MyAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects
MyAction.TargetObjectsCriteria = "DueDate <= LocalDateTimeNow()"
The following image demonstrates how the TargetObjectsCriteria property is specified in the Model Editor:
Get Criteria String via the Criteria Property Editor
XAF provides the following string property editors, intended to construct filter criteria visually (see Criteria Properties):
Windows Forms Criteria Property Editors:
ASP.NET Criteria Property Editors:
These property editors can be used to represent string properties decorated with the CriteriaOptionsAttribute attribute. The following image illustrates the PopupCriteriaPropertyEditor:
The corresponding criteria string is:
[DueDate] <= LocalDateTimeNow() and [Status] = 'Completed'
The important concept is that the Criteria Property Editors can generate criteria strings containing the XAF-specific Object Parameters. Such criteria strings cannot be parsed using the CriteriaOperator.Parse method, provided by XPO. You should use the GetCriteriaOperator method exposed by the CriteriaEditorHelper helper class, to get the corresponding CriteriaOperator object: