Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Implement Custom Context Navigation

  • 3 minutes to read

The Navigation System allows you to create custom navigation items at runtime based on specific conditions (context).

This topic explains how to populate the Help Document item with custom child items.

Application with context navigation items

#Step 1. Create the HelpDocument Business Class

Create the HelpDocument business class with Title and Text properties.

using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
// ...

[DefaultClassOptions, ImageName("BO_Report")]
public class HelpDocument : BaseObject {

    public virtual string Title { get; set; }
    // A document's text can be very long, so add FieldSizeAttribute and set its value to `Unlimited`
    [FieldSize(FieldSizeAttribute.Unlimited)]
    public virtual string Text { get; set; }
}

#Step 2. Create a Controller and Subscribe to the NavigationItemCreated Event

  1. Add a Window Controller to the application.
  2. Subscribe to the ShowNavigationItemController.NavigationItemCreated event that occurs after an item is created in the navigation control and allows you to modify the item. Unsubscribe from the event to prevent memory leaks.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.SystemModule;
//...
public class TaskBasedHelpController : WindowController {
    private ShowNavigationItemController navigationController;
    protected override void OnFrameAssigned() {
        UnsubscribeFromEvents();
        base.OnFrameAssigned();
        navigationController = Frame.GetController<ShowNavigationItemController>();
        if(navigationController != null) {
            navigationController.NavigationItemCreated += navigationItemCreated;
        }
    }
    private void UnsubscribeFromEvents() {
        if(navigationController != null) {
            navigationController.NavigationItemCreated -= navigationItemCreated;
            navigationController = null;
        }
    }
    protected override void Dispose(bool disposing) {
        UnsubscribeFromEvents();
        base.Dispose(disposing);
    }
}

#Step 3. Handle the NavigationItemCreated Event

  1. In the NavigationItemCreated event handler, add a child Items folder to the Help Document item.
  2. For every HelpDocument object, create a detail view.
  3. For every detail view, create a navigation action (ChoiceActionItem) that displays the detail view.
  4. Add the actions in the Items folder.
C#
void navigationItemCreated(object sender, NavigationItemCreatedEventArgs e) {
    var docType = typeof(HelpDocument);
    if (((IModelNavigationItem)e.NavigationItem.Model).View is IModelListView listViewNode
        && listViewNode.Id == Application.GetListViewId(docType)) {
        var os = Application.CreateObjectSpace<HelpDocument>();
        var docs = os.GetObjects<HelpDocument>();
        if (docs.Any()) {
            var docsSubGroup = new ChoiceActionItem() { Caption = "Items", ImageName = "BO_Folder" };
            e.NavigationItem.Items.Add(docsSubGroup);
            foreach (var doc in docs) {
                var keyString = os.GetKeyValueAsString(doc);
                var shortcut = new ViewShortcut(typeof(HelpDocument), keyString, Application.GetDetailViewId(docType));
                var docItem = new ChoiceActionItem(keyString, doc.Title, shortcut) {
                    ImageName = e.NavigationItem.ImageName
                };
                docsSubGroup.Items.Add(docItem);
            }
        }
    }
}

Note

This topic demonstrates a solution that does not re-create the Navigation System items after HelpDocument objects are modified. You can call the RecreateNavigationItems() method to update the navigation item structure when modifying data.

For more information, refer to the following GitHub example: How to show the number of List View items in the Navigation Control.

See Also