Example T943913
Visible to All Users

XAF - How to show the number of nested List View items in tab captions

This example demonstrates how to create a controller to show the number of nested List View items in tab captions. You can add, delete, link, or unlink items in nested List Views, or move between different records in the Detail View. The controller automatically updates the record count in the tab caption.

image

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

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

Example Code

EFCore/TabCountEF/TabCountEF.Module/DetailViewControllerHelper.cs
C#
namespace TabCount.Module { public static class DetailViewControllerHelper { public static string ClearItemCountInTabCaption(string caption) { int index = caption.IndexOf('('); if (index != -1) { return caption.Remove(index - 1); } return caption; } public static string AddItemCountToTabCaption(string caption, int count) { return $"{caption} ({count})"; } } }
EFCore/TabCountEF/TabCountEF.Blazor.Server/Controllers/ContactDetailViewBlazorController.cs
C#
using DevExpress.ExpressApp; using DevExpress.ExpressApp.Blazor.Components.Models; using DevExpress.ExpressApp.Blazor.Layout; using DevExpress.ExpressApp.Editors; using DevExpress.ExpressApp.Model; using MySolution.Module.BusinessObjects; using System; using System.ComponentModel; using TabCount.Module; namespace DetailViewTabCount.Module.Blazor.Controllers { public class ContactDetailViewBlazorController : ObjectViewController<DetailView, Contact> { protected override void OnActivated() { base.OnActivated(); View.DelayedItemsInitialization = false; ((BlazorLayoutManager)View.LayoutManager).ItemCreated += DxFormLayoutItemController_ItemCreated; } private void DxFormLayoutItemController_ItemCreated(object sender, BlazorLayoutManager.ItemCreatedEventArgs e) { if (e.LayoutControlItem is DxFormLayoutTabPageModel layoutGroup && e.ModelLayoutElement.Parent is IModelTabbedGroup) { foreach (var item in (IModelLayoutGroup)e.ModelLayoutElement) { if (item is IModelLayoutViewItem layoutViewItem && View.FindItem(layoutViewItem.ViewItem.Id) is ListPropertyEditor propertyEditor) { propertyEditor.Frame.GetController<NestedListViewTabCountController>().Initialize(layoutGroup); propertyEditor.ValueRead += (s, e) => { propertyEditor.Frame.GetController<NestedListViewTabCountController>().SubscribeToListChanged(); }; } } } } protected override void OnDeactivated() { ((BlazorLayoutManager)View.LayoutManager).ItemCreated -= DxFormLayoutItemController_ItemCreated; base.OnDeactivated(); } } public class NestedListViewTabCountController : ViewController<ListView> { private DxFormLayoutTabPageModel layoutGroup; public NestedListViewTabCountController() { TargetViewNesting = Nesting.Nested; } protected override void OnActivated() { base.OnActivated(); UpdateLayoutGroupText(); View.CollectionSource.CollectionChanging += CollectionSource_CollectionChanging; View.CollectionSource.CollectionChanged += CollectionSource_CollectionChanged; View.CollectionSource.CollectionReloaded += CollectionSource_CollectionReloaded; SubscribeToListChanged(); } internal void SubscribeToListChanged() { if (GetBindingList(View.CollectionSource.Collection) is IBindingList bindingList) { bindingList.ListChanged += CollectionSourceBindingList_ListChanged; } } public void Initialize(DxFormLayoutTabPageModel layoutGroup) { this.layoutGroup = layoutGroup; UpdateLayoutGroupText(); } private void CollectionSource_CollectionChanging(object sender, EventArgs e) { UnsubscribeFromListChanged(); } private void CollectionSource_CollectionChanged(object sender, EventArgs e) { UpdateLayoutGroupText(); SubscribeToListChanged(); } private void CollectionSource_CollectionReloaded(object sender, EventArgs e) { UpdateLayoutGroupText(); } private void CollectionSourceBindingList_ListChanged(object sender, ListChangedEventArgs e) { UpdateLayoutGroupText(); } private void UpdateLayoutGroupText() { if (layoutGroup != null) { int count = View.CollectionSource.GetCount(); layoutGroup.Caption = DetailViewControllerHelper.ClearItemCountInTabCaption(layoutGroup.Caption); if (count > 0) { layoutGroup.Caption = DetailViewControllerHelper.AddItemCountToTabCaption(layoutGroup.Caption, count); } } } protected override void OnDeactivated() { View.CollectionSource.CollectionChanging -= CollectionSource_CollectionChanging; View.CollectionSource.CollectionChanged -= CollectionSource_CollectionChanged; View.CollectionSource.CollectionReloaded -= CollectionSource_CollectionReloaded; UnsubscribeFromListChanged(); base.OnDeactivated(); } private void UnsubscribeFromListChanged() { if (GetBindingList(View.CollectionSource.Collection) is IBindingList bindingList) { bindingList.ListChanged -= CollectionSourceBindingList_ListChanged; } } private static IBindingList GetBindingList(object collection) { switch (collection) { case IBindingList bindingList: return bindingList; case IListSource listSource: return listSource.GetList() as IBindingList; default: return null; } } } }
EFCore/TabCountEF/TabCountEF.Win/Controllers/ContactDetailViewWinController.cs
C#
using System; using System.ComponentModel; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Editors; using DevExpress.ExpressApp.Model; using DevExpress.ExpressApp.Win.Layout; using DevExpress.XtraLayout; using MySolution.Module.BusinessObjects; using TabCount.Module; namespace DetailViewTabCount.Module.Win.Controllers { public class ContactDetailViewWinController : ObjectViewController<DetailView, Contact> { protected override void OnActivated() { base.OnActivated(); View.DelayedItemsInitialization = false; ((WinLayoutManager)View.LayoutManager).ItemCreated += DetailViewTabCountController_ItemCreated; } private void DetailViewTabCountController_ItemCreated(object sender, ItemCreatedEventArgs e) { if(e.Item is LayoutGroup layoutGroup && e.ModelLayoutElement.Parent is IModelTabbedGroup) { foreach(var item in (IModelLayoutGroup)e.ModelLayoutElement) { if(item is IModelLayoutViewItem layoutViewItem && View.FindItem(layoutViewItem.ViewItem.Id) is ListPropertyEditor propertyEditor) { propertyEditor.Frame.GetController<NestedListViewTabCountController>().Initialize(layoutGroup); propertyEditor.ValueRead += (s, e) => { propertyEditor.Frame.GetController<NestedListViewTabCountController>().SubscribeToListChanged(); }; } } } } protected override void OnDeactivated() { ((WinLayoutManager)View.LayoutManager).ItemCreated -= DetailViewTabCountController_ItemCreated; base.OnDeactivated(); } } public class NestedListViewTabCountController : ViewController<ListView> { private LayoutGroup layoutGroup; public NestedListViewTabCountController() { TargetViewNesting = Nesting.Nested; } protected override void OnActivated() { base.OnActivated(); UpdateLayoutGroupText(); View.CollectionSource.CollectionChanging += CollectionSource_CollectionChanging; View.CollectionSource.CollectionChanged += CollectionSource_CollectionChanged; View.CollectionSource.CollectionReloaded += CollectionSource_CollectionReloaded; SubscribeToListChanged(); } internal void SubscribeToListChanged() { if(GetBindingList(View.CollectionSource.Collection) is IBindingList bindingList) { bindingList.ListChanged += CollectionSourceBindingList_ListChanged; } } public void Initialize(LayoutGroup layoutGroup) { this.layoutGroup = layoutGroup; UpdateLayoutGroupText(); } private void CollectionSource_CollectionChanging(object sender, EventArgs e) { UnsubscribeFromListChanged(); } private void CollectionSource_CollectionChanged(object sender, EventArgs e) { UpdateLayoutGroupText(); SubscribeToListChanged(); } private void CollectionSource_CollectionReloaded(object sender, EventArgs e) { UpdateLayoutGroupText(); } private void CollectionSourceBindingList_ListChanged(object sender, ListChangedEventArgs e) { UpdateLayoutGroupText(); } private void UpdateLayoutGroupText() { if(layoutGroup != null) { int count = View.CollectionSource.GetCount(); layoutGroup.Text = DetailViewControllerHelper.ClearItemCountInTabCaption(layoutGroup.Text); if(count > 0) { layoutGroup.Text = DetailViewControllerHelper.AddItemCountToTabCaption(layoutGroup.Text, count); } } } protected override void OnDeactivated() { View.CollectionSource.CollectionChanging -= CollectionSource_CollectionChanging; View.CollectionSource.CollectionChanged -= CollectionSource_CollectionChanged; View.CollectionSource.CollectionReloaded -= CollectionSource_CollectionReloaded; UnsubscribeFromListChanged(); base.OnDeactivated(); } private void UnsubscribeFromListChanged() { if(GetBindingList(View.CollectionSource.Collection) is IBindingList bindingList) { bindingList.ListChanged -= CollectionSourceBindingList_ListChanged; } } private static IBindingList GetBindingList(object collection) { switch(collection) { case IBindingList bindingList: return bindingList; case IListSource listSource: return listSource.GetList() as IBindingList; default: return null; } } } }

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.