Example E4916
Visible to All Users

XAF - How to implement dependent views in a DashboardView (filter based on selection)

This example illustrates how to filter a ListView displayed in a DashboardView based on another ListView's selection.

Scenario

When a DashboardView contains several list views, it is often required to make them dependent, e.g. display items of one ListView based on items or selection of another ListView.

chrome_WHVRxufQHv

Implementation Details

  1. Add a new ViewController to the YourSolutionName.Module project. For more information, refer to the following file: DashboardFilterController.cs.
  2. In the OnActivated method, retrieve DashboardViewItems via the FindItem method, and subscribe to the ControlCreated event of a DashboardViewItem whose ListView is used to filter data (hereinafter referred to as SourceView).
  3. In the ControlCreated event handler retrieve the SourceView via the DashboardViewItem.InnerView property and subscribe to its SelectionChanged event.
  4. In the SelectionChanged event handler, retrieve the View to be filtered (hereinafter referred to as TargetView) in the same way as the previous step.
  5. To get an object that is used for filtering, use the ListView.CurrentObject property.
  6. You can now add CriteriaOperator to the TargetView.CollectionSource.Criteria dictionary to filter the TargetView. In this example, we created an InOperator that uses SourceView objects to filter the AssignedTo column.

Files to Review

Documentation

ASP.NET WebForms Version

You can find the ASP.NET WebForms version of this example in the following branch: DevExpress-Examples/xaf-how-to-implement-dependent-views-in-a-dashboardview-filter-based-on-selection at 17.2.3+

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

EFCore/DependentDashboardEF/DependentDashboardEF.Module/Controllers/DashboardFilterController.cs
C#
using System; using System.Linq; using DevExpress.ExpressApp; using DevExpress.Data.Filtering; using System.Collections.Generic; using DevExpress.ExpressApp.Editors; using DevExpress.ExpressApp.SystemModule; using DXApplication4.Module.BusinessObjects; namespace Solution3.Module.Controllers { // For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic. public class DashboardFilterController : ViewController<DashboardView> { private const string DashboardViewId = "MyDashboardView"; private DashboardViewItem SourceItem; private DashboardViewItem TargetItem; private const string CriteriaName = "Test"; private void FilterDetailListView(ListView masterListView, ListView detailListView) { detailListView.CollectionSource.Criteria.Clear(); List<object> searchedObjects = new List<object>(); foreach (object obj in masterListView.SelectedObjects) { searchedObjects.Add(detailListView.ObjectSpace.GetKeyValue(obj)); } if (searchedObjects.Count > 0) { detailListView.CollectionSource.Criteria[CriteriaName] = CriteriaOperator.FromLambda<MyTask>(x => searchedObjects.Contains(x.AssignedTo.ID)); } } private void SourceItem_ControlCreated(object sender, EventArgs e) { DashboardViewItem dashboardItem = (DashboardViewItem)sender; ListView innerListView = dashboardItem.InnerView as ListView; if (innerListView != null) { innerListView.SelectionChanged -= innerListView_SelectionChanged; innerListView.SelectionChanged += innerListView_SelectionChanged; } } private void innerListView_SelectionChanged(object sender, EventArgs e) { FilterDetailListView((ListView)SourceItem.InnerView, (ListView)TargetItem.InnerView); } private void DisableNavigationActions(Frame frame) { RecordsNavigationController recordsNavigationController = frame.GetController<RecordsNavigationController>(); if (recordsNavigationController != null) { recordsNavigationController.Active.SetItemValue("DashboardFiltering", false); } } protected override void OnActivated() { base.OnActivated(); if (View.Id == DashboardViewId) { SourceItem = (DashboardViewItem)View.FindItem(FilterSourceID); TargetItem = (DashboardViewItem)View.FindItem(FilterTargetId); if (SourceItem != null) { SourceItem.ControlCreated += SourceItem_ControlCreated; } if (TargetItem != null) { if (TargetItem.Frame != null) { DisableNavigationActions(TargetItem.Frame); } else { TargetItem.ControlCreated += (s, e) => { DisableNavigationActions(TargetItem.Frame); }; } } } } protected override void OnDeactivated() { if (SourceItem != null) { SourceItem.ControlCreated -= SourceItem_ControlCreated; SourceItem = null; } TargetItem = null; base.OnDeactivated(); } public DashboardFilterController() { FilterSourceID = "ContactView"; FilterTargetId = "MyTaskView"; } public string FilterSourceID { get; set; } public string FilterTargetId { get; set; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.