How to: Make the FullTextSearch Action Search Within Required Properties 如何:使用全文搜索按钮在需要的属性中进行全文搜索
This topic demonstrates how to customize the FullTextSearch Action's behavior. This Action filters the current List View by setting criteria for its collection data source. According to the criteria, the object's properties must contain individual words from the word combination typed by an end-user. Reference several techniques on how to modify the Action's behavior, in the FilterController.FullTextFilterAction member description. Here, you will see how to use one of these techniques. We will specify a custom list of the properties that will be used to generate the filter criterion for the current List View.
本主题演示如何自定义全文搜索操作的行为。此操作通过为其集合数据源设置条件来筛选当前列表视图。根据条件,对象的属性必须包含最终用户键入的单词组合中的单个单词。在筛选器控制器.FullTextFilterAction 成员描述中,引用有关如何修改操作行为的几种技术。在这里,您将看到如何使用这些技术之一。我们将指定将用于生成当前列表视图的筛选条件的属性的自定义列表。
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E231
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E231
.
To specify a custom list of the properties that will be used in the search, the Filter Controller, which contains the FullTextSearch Action, exposes the FilterController.CustomGetFullTextSearchProperties event. To handle this event, we will add a new View Controller, and subscribe to its Controller.Activated event.
要指定将在搜索中使用的属性的自定义列表,包含"完全文本搜索"操作的筛选器控制器将公开筛选器控制器。为了处理此事件,我们将添加新的视图控制器,并订阅其控制器.激活事件。
In the CustomGetFullTextSearchProperties event handler, assign a list of any required properties to the CustomGetFullTextSearchPropertiesEventArgs.Properties parameter. In addition, set the CustomGetFullTextSearchPropertiesEventArgs.Handled parameter to true, to indicate that other properties must not be added to the list. In this example, we will add only the "LastName" property of the Person class. So, we will activate our Controller for Person List Views only. The MyFilterController is shown below:
在"自定义获取全文搜索属性"事件处理程序中,将任何必需属性的列表分配给"自定义获取全文搜索属性事件"参数。此外,将"自定义获取全文搜索属性事件"参数设置为 true,以指示不得将其他属性添加到列表中。在此示例中,我们将仅添加 Person 类的"姓氏"属性。因此,我们将仅激活人员列表视图的控制器。"我的过滤器控制器"如下所示:
using System; using System.Collections.Generic; using DevExpress.ExpressApp; using DevExpress.ExpressApp.SystemModule; //... public partial class MyFilterController : ViewController { public MyFilterController() { InitializeComponent(); RegisterActions(components); this.TargetObjectType = typeof(DevExpress.Persistent.BaseImpl.Person); } private void MyFilterController_Activated(object sender, EventArgs e) { FilterController standardFilterController = Frame.GetController<FilterController>(); if(standardFilterController != null) { standardFilterController.CustomGetFullTextSearchProperties += new EventHandler<CustomGetFullTextSearchPropertiesEventArgs>(standardFilterController_CustomGetFullTextSearchProperties); } } void standardFilterController_CustomGetFullTextSearchProperties(object sender, CustomGetFullTextSearchPropertiesEventArgs e) { foreach(string property in GetFullTextSearchProperties()) { e.Properties.Add(property); } e.Handled = true; } private List<string> GetFullTextSearchProperties() { List<string> searchProperties = new List<string>(); searchProperties.Add("LastName"); return searchProperties; } }
The following image demonstrate that the MyFilterController works:
下图演示了"我的过滤器控制器"的工作原理:
Filtration may be impossible in case one or more property is of a particular type. For example, in Server mode, filtering by properties of the DateTimetype, which are stored in a database as columns of the datetime date type, may cause exceptions. To avoid them, ensure that the inputed text can be converted to the corresponding type and this converted value is within the acceptable range, and exclude a property from the default filter list if necessary. The following code demonstrates the Controller implementing this logic.
如果一个或多个属性是特定类型的,则过滤可能是不可能的。例如,在服务器模式下,按 DateTimetype 的属性进行筛选(这些属性作为日期日期类型的列存储在数据库中)可能会导致异常。为了避免它们,请确保输入的文本可以转换为相应的类型,并且此转换值在可接受的范围内,并在必要时从默认筛选器列表中排除属性。以下代码演示了实现此逻辑的控制器。
using System; using System.Collections.Generic; using DevExpress.ExpressApp; using DevExpress.ExpressApp.SystemModule; using DevExpress.ExpressApp.DC; using DevExpress.Data.Summary; using System.Data.SqlTypes; //... public class MyFilterController : ViewController<ListView> { private static readonly DateTime minDate; private static readonly DateTime maxDate; static MyFilterController() { minDate = (DateTime)SqlDateTime.MinValue; maxDate = (DateTime)SqlDateTime.MaxValue; } private Boolean CanFilter(string propertyName, string filterText) { IMemberInfo memberInfo = View.ObjectTypeInfo.FindMember(propertyName); if (SummaryItemTypeHelper.IsDateTime(memberInfo.MemberType)) { DateTime? convertedFilter = null; try { convertedFilter = Convert.ChangeType(filterText, typeof(DateTime)) as DateTime?; } catch { return false; } if (convertedFilter.HasValue) { if ((convertedFilter.Value < minDate) || (convertedFilter.Value > maxDate)) { return false; } } } return true; } private ICollection<string> GetProcessedRequiredProperties(ICollection<string> searchProperties, string filterText) { List<string> result = new List<string>(); foreach (string propertyName in searchProperties) { if (CanFilter(propertyName, filterText)) { result.Add(propertyName); } } return result; } private void FilterController_CustomGetFullTextSearchProperties(object sender, CustomGetFullTextSearchPropertiesEventArgs e) { string filterText = ((FilterController)sender).FullTextFilterAction.Value as string; if (!string.IsNullOrEmpty(filterText)) { ICollection<string> searchProperties = GetProcessedRequiredProperties(((FilterController)sender).GetFullTextSearchProperties(), filterText); e.Properties.AddRange(searchProperties); e.Handled = true; } } protected override void OnActivated() { base.OnActivated(); FilterController filterController = Frame.GetController<FilterController>(); if (filterController != null) { filterController.CustomGetFullTextSearchProperties += FilterController_CustomGetFullTextSearchProperties; } } protected override void OnDeactivated() { FilterController filterController = Frame.GetController<FilterController>(); if (filterController != null) { filterController.CustomGetFullTextSearchProperties -= FilterController_CustomGetFullTextSearchProperties; } base.OnDeactivated(); } }
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端