How to: Use Criteria Property Editors 如何:使用条件属性编辑器
This topic details the specifics of using Criteria Property Editors in an XAF application. An application that allows end-users to design and save filtering criteria at runtime is demonstrated here.
本主题详细介绍了在 XAF 应用程序中使用条件属性编辑器的详细信息。此处演示了允许最终用户在运行时设计和保存筛选条件的应用程序。
Note 注意
Mobile applications do not provide any Criteria Property Editor, so the approach described in this topic cannot be implemented in the Mobile platform.
移动应用程序不提供任何条件属性编辑器,因此本主题中描述的方法无法在移动平台中实现。
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E932
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E932
.
To create the sample application, do the following:
- Define business classes whose List Views will later be filtered.
- Define the FilteringCriterion business class that represents filtering criteria for the Product's List View.
- Create a custom View Controller that contains an Action used to filter the List View.
要创建示例应用程序,请执行以下操作:
1. 定义稍后将筛选其列表视图的业务类。
2. 定义筛选标准业务类,表示产品列表视图的筛选条件。
3. 创建自定义视图控制器,其中包含用于筛选列表视图的操作。
To begin building the sample application, create a new XAF application solution named HowToUseCriteriaPropertyEditors using the DevExpress v19.2 XAF Solution Wizard Template Gallery.Built-in Visual Studio Templates.
要开始构建示例应用程序,请使用 DevExpress v19.2 XAF 解决方案向导模板库创建名为"如何使用标准属性编辑器"的新 XAF 应用程序解决方案。
Step 1 - Add the Product and Person Business Classes
步骤 1 - 添加产品和人员业务课程
Right-click the HowToUseCriteriaPropertyEditors.Module project, and choose Add DevExpress Item | New Item... to invoke Template Gallery. Then select the XAF Business Object | XPO Business Object project item, name it Product, and press Add Item. You will get an automatically generated code file with a single class declaration. Replace the automatically generated class declaration with the following code, defining a class with four persistent properties of different types:
右键单击"如何使用标准属性编辑器.模块"项目,然后选择"添加开发快递项目"新项目...以调用模板库。然后选择 XAF 业务对象 |XPO 业务对象项目项,将其命名为"产品",然后按"添加项"。您将获得一个自动生成的代码文件,该文件带有单个类声明。将自动生成的类声明替换为以下代码,使用四个不同类型的持久属性定义类:
[DefaultClassOptions, ImageName("BO_Product")] public class Product : BaseObject { public Product(Session session) : base(session) { } public string Name { get { return GetPropertyValue<string>(nameof(Name)); } set { SetPropertyValue<string>(nameof(Name), value); } } public double Price { get { return GetPropertyValue<double>(nameof(Price)); } set { SetPropertyValue<double>(nameof(Price), value); } } public int Quantity { get { return GetPropertyValue<int>(nameof(Quantity)); } set { SetPropertyValue<int>(nameof(Quantity), value); } } public Person Manager { get { return GetPropertyValue<Person>(nameof(Manager)); } set { SetPropertyValue<Person>(nameof(Manager), value); } } }
The Manager reference property is of the Person type, that is the class from the Business Class Library. Add a Person item to the ShowNavigationItem and to the New action, via the Model Editor (see Add an Item to the Navigation Control and Add an Item to the New Action).
Manager 引用属性是 Person 类型,即来自 Business 类库的类。通过模型编辑器将人员项目添加到"显示导航项"和"新建"操作中(请参阅向导航控件添加项目并将项目添加到"新建操作")。
Step 2 - Define the FilteringCriterion Business Class
步骤 2 - 定义筛选标准业务部门类
Add the Domain Object named FilteringCriterion to the HowToUseCriteriaPropertyEditors.Module project, and replace the automatically generated class declaration with the following code:
将名为"筛选标准"的域对象添加到"如何使用标准属性编辑器.模块"项目,并将自动生成的类声明替换为以下代码:
using DevExpress.ExpressApp.Editors; using DevExpress.ExpressApp.Utils; using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl; using DevExpress.Xpo; using System; using System.ComponentModel; //... [DefaultClassOptions, ImageName("Action_Filter")] public class FilteringCriterion : BaseObject { public FilteringCriterion(Session session) : base(session) { } public string Description { get { return GetPropertyValue<string>(nameof(Description)); } set { SetPropertyValue<string>(nameof(Description), value); } } [ValueConverter(typeof(TypeToStringConverter)), ImmediatePostData] [TypeConverter(typeof(LocalizedClassInfoTypeConverter))] public Type ObjectType { get { return GetPropertyValue<Type>(nameof(ObjectType)); } set { SetPropertyValue<Type>(nameof(ObjectType), value); Criterion = String.Empty; } } [CriteriaOptions("ObjectType"), Size(SizeAttribute.Unlimited)] [EditorAlias(EditorAliases.PopupCriteriaPropertyEditor)] public string Criterion { get { return GetPropertyValue<string>(nameof(Criterion)); } set { SetPropertyValue<string>(nameof(Criterion), value); } } }
Objects of this class represent filtering criteria. The Description field contains the text that describes a criterion. These descriptions will be used to fill a drop-down list of possible filters. The Criterion property holds a criterion. To use the built-in XAF Criteria Property Editors, the property that holds a criterion must have the CriteriaOptionsAttribute applied. The attribute's parameter is the name of an additional Type property, which specifies the objects' type used in the construction of the criterion. In this sample, it is the ObjectType property. Additionally, the CriteriaOptions attribute changes the default Property Editor for the property:
- CriteriaPropertyEditor is used in Windows Forms applications
- ASPxCriteriaPropertyEditor is used in ASP.NET Web applications
此类的对象表示筛选条件。"描述"字段包含描述条件的文本。这些描述将用于填充可能筛选器的下拉列表。"标准"属性包含一个条件。要使用内置的 XAF 条件属性编辑器,保存条件的属性必须应用"条件选项属性"。属性的参数是附加 Type 属性的名称,该属性指定在条件构造中使用的对象类型。在此示例中,它是 ObjectType 属性。此外,CriteriaOptions 属性更改属性的默认属性编辑器:
- 条件属性编辑器用于 Windows 窗体应用程序
- ASPx 标准属性编辑器用于ASP.NET Web 应用程序
Step 3 - Create a Custom List View Controller
步骤 3 - 创建自定义列表视图控制器
Add the following CriteriaController class to the HowToUseCriteriaPropertyEditors.Module project.
将以下条件控制器类添加到"如何使用标准属性编辑器.模块"项目。
using DevExpress.ExpressApp; using DevExpress.ExpressApp.Actions; using DevExpress.Persistent.Base; using DevExpress.ExpressApp.Editors; using DevExpress.Data.Filtering; using DevExpress.ExpressApp.Templates; // ... public class CriteriaController : ObjectViewController { private SingleChoiceAction filteringCriterionAction; public CriteriaController() { filteringCriterionAction = new SingleChoiceAction( this, "FilteringCriterion", PredefinedCategory.Filters); filteringCriterionAction.Execute += new DevExpress.ExpressApp.Actions.SingleChoiceActionExecuteEventHandler(this.FilteringCriterionAction_Execute); TargetViewType = ViewType.ListView; } 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) { ((ListView)View).CollectionSource.BeginUpdateCriteria(); ((ListView)View).CollectionSource.Criteria.Clear(); ((ListView)View).CollectionSource.Criteria[e.SelectedChoiceActionItem.Caption] = CriteriaEditorHelper.GetCriteriaOperator( e.SelectedChoiceActionItem.Data as string, View.ObjectTypeInfo.Type, ObjectSpace); ((ListView)View).CollectionSource.EndUpdateCriteria(); } }
This Controller adds the FilteringCriterion SingleChoiceAction and populates the Action's dropdown list with all the existing FilterCriterion objects whose ObjectType matches the type of objects displayed in the current List View.
此控制器添加筛选标准单一选择操作,并将操作的下拉列表填充所有现有筛选标准对象,其 ObjectType 与当前列表视图中显示的对象类型匹配。
The FilteringCriterion Action applies the criterion selected in the dropdown list to the List View. Note that 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 represents 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.筛选.标准运算符对象,您需要调用静态标准编辑器Helper.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.
在此方案中,不应使用标准操作员.Parse 方法。条件属性编辑器可以生成包含对象参数的条件字符串,这些字符串特定于 XAF,不能由 CriteriaOperator.Parse 解析。
Now you can run the Windows Forms or ASP.NET application, create several Person, Product and FilterCriteria objects, and then try the FilteringCriterion action.
现在,您可以运行 Windows 窗体或ASP.NET应用程序,创建多个人员、产品和筛选标准对象,然后尝试筛选标准操作。
The Criteria Property Editors are also demonstrated in the Property Editors | Criteria Properties section in the Feature Center demo installed with XAF. The Feature Center demo is installed in %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\FeatureCenter by default. The ASP.NET version of this demo is available online at http://demos.devexpress.com/XAF/FeatureCenter/
属性编辑器中还演示了条件属性编辑器 |与 XAF 一起安装的功能中心演示中"条件属性"部分。功能中心演示安装在%PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\FeatureCenter by default. The ASP.NET version of this demo is available online at http://demos.devexpress.com/XAF/FeatureCenter/
.
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码: