How to: Filter a Link Dialog's List View 如何:筛选链接对话框的列表视图

This topic details how to filter List Views in the Link Action's pop-up Window. Sometimes, you may need to filter the List View based on specific criteria. Oftentimes, you may need to make the List View data source dependent on property editor values of the source Detail View. The eXpressApp Framework provides the DataSourcePropertyAttribute and DataSourceCriteriaAttribute for this purpose. These attributes can be applied both in code and in the Application Model, via the Model Editor. This topic demonstrates how to apply the DataSourceProperty and DataSourceCriteria attributes in code, using a special data source generated on the fly.

本主题详细介绍了如何筛选链接操作的弹出窗口中的"列表视图"。有时,您可能需要根据特定条件筛选列表视图。通常,您可能需要使列表视图数据源依赖于源详细信息视图的属性编辑器值。eXpressApp 框架为此提供了数据源属性和数据源标准属性。这些属性可以通过模型编辑器在代码和应用程序模型中应用。本主题演示如何使用动态生成的特殊数据源在代码中应用 DataSourceProperty 和 DataSourceCriteria 属性。

Note 注意
The DataSourceCriteriaAttribute and DataSourcePropertyAttribute attributes can also be used to filter Lookup Property Editor List Views (see How to: Implement Cascading Filtering for Lookup List Views).
DataSourceCriteria属性属性和DataSourceProperty属性属性属性属性还可用于筛选查找属性编辑器列表视图(请参阅:如何实现查找列表视图的级联筛选)。
 
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E235
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E235

Initial Implementation

初步实施

Initially, a Contact includes a Position and Task collection. Contact and Task objects are related by the Many-to-Many relationship. Contact and Position objects are related by the One-to-Many relationship. The following code shows how these classes can be implemented:

最初,联系人包括职位和任务集合。联系人和任务对象由多对多关系相关。联系人和位置对象由一对多关系相关。以下代码演示如何实现这些类:

using System.ComponentModel;
using DevExpress.Data.Filtering;
//...
[DefaultClassOptions]
public class Contact : Person {
   public Contact(Session session) : base(session) {}
   private Position position;
   public Position Position {
      get {
         return position;
      }
      set {
         SetPropertyValue(nameof(Position), ref position, value);
      }
   }
   [Association("Contact-DemoTask")]
   public XPCollection<DemoTask> Tasks {
      get {
         return GetCollection<DemoTask>(nameof(Tasks));
      }
   }
}
[DefaultClassOptions]
[System.ComponentModel.DefaultProperty(nameof(Title))]
public class Position : BaseObject {
   public Position(Session session) : base(session) {}
   private string title;
   public string Title {
      get {
         return title;
      }
      set {
         SetPropertyValue(nameof(Title), ref title, value);
      }
   }
}
[DefaultClassOptions]
public class DemoTask : Task {
   private Priority priority;
   public DemoTask(Session session): base(session) {}
   public Priority Priority {
      get {
         return priority;
      }
      set {
         SetPropertyValue(nameof(Priority), ref priority, value);
      }
   }
   [Association("Contact-DemoTask")]
   public XPCollection<Contact> Contacts {
      get {
         return GetCollection<Contact>(nameof(Contacts));
      }
   }
   //...
}
public enum Priority {
   Low = 0,
   Normal = 1,
   High = 2
}

The image below demonstrates the Contact Detail View in a Windows Forms application:

下图演示了 Windows 窗体应用程序中的"联系人详细信息"视图:

HowToFilterLinkDialogListView

Here, the Link Action's pop-up Window displays a List View with all existing Task objects. However, certain scenarios may require a filtered collection to be displayed by this List View.

在这里,链接操作的弹出窗口显示包含所有现有任务对象的列表视图。但是,某些方案可能需要此列表视图显示筛选的集合。

自定义链接操作列表视图数据源

Assume that high priority tasks can only be assigned to managers. The List View in the Link Action's pop-up window should be filtered according to the criteria [Priority] = "High" to implement this. In this instance, the DataSourceCriteriaAttribute can be applied to the Contact.Tasks property. However, we decided that this List View should be filtered only when the current Contact object's Position is manager. Thus, we need to generate a filtered collection of Task objects only when the Title property of the current Contact's Position is set to "Manager". In all other instances, the List View will contain all existing Task objects. For this purpose, the DataSourcePropertyAttribute is very helpful. We will apply it to the Contact.Tasks property and pass our new collection as the data source. The following code demonstrates how to do this:

假设高优先级任务只能分配给经理。链接操作弹出窗口中的列表视图应根据条件 [优先级] ="高"进行筛选以实现此。在这种情况下,DataSource 标准属性可以应用于 Contact.Tasks 属性。但是,我们决定仅当当前联系人对象的位置为管理器时,才应筛选此列表视图。因此,仅当当前联系人位置的 Title 属性设置为"管理器"时,我们才需要生成已筛选的 Task 对象集合。在所有其他情况下,列表视图将包含所有现有的任务对象。为此,DataSourceProperty 属性非常有用。我们将将其应用于 Contact.Tasks 属性,并将新集合作为数据源传递。以下代码演示如何执行此操作:

using System.ComponentModel;
using DevExpress.Data.Filtering;
//...
[DefaultClassOptions]
public class Contact : Person {
   //...
   [Association("Contact-DemoTask")]
   // Set the AvailableTasks collection as the data source for the Tasks property
   [DataSourceProperty(nameof(AvailableTasks))]
   public XPCollection<DemoTask> Tasks {
      get {
         return GetCollection<DemoTask>(nameof(Tasks));
      }
   }
   private XPCollection<DemoTask> availableTasks;
   [Browsable(false)] // Prohibits showing the AvailableTasks collection separately
   public XPCollection<DemoTask> AvailableTasks {
      get {
         if(availableTasks == null) {
            // Retrieve all Task objects
            availableTasks = new XPCollection<DemoTask>(Session);
         }
      // Filter the retrieved collection according to the current conditions
      RefreshAvailableTasks();
      // Return the filtered collection of Task objects
      return availableTasks;
      }
   }
   private void RefreshAvailableTasks() {
      if(availableTasks == null)
         return;
      if((Position != null) && (Position.Title == "Manager")) {
         //Filter the collection
         availableTasks.Criteria = CriteriaOperator.Parse("[Priority] = 'High'");
      }
      else {
         //Remove the applied filter
         availableTasks.Criteria = null;
      }
   }
   private Position position;
   public Position Position {
      get {
         return position;
      }
      set {
         SetPropertyValue(nameof(Position), ref position, value);
         // Refresh the Tasks property data source
         RefreshAvailableTasks();
      }
   }
}

The following image shows a Contact Detail View and the Link Action's filtered List View:

下图显示了联系人详细信息视图和链接操作的筛选列表视图:

HowToFilterLinkDialogListView_Result

 
原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Filter-a-Link-Dialog-s-List-View.html