Implement Custom Context Navigation
- 3 minutes to read
In This Article
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.
#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
- Add a Window Controller to the application.
- 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
- In the
NavigationItemCreated
event handler, add a child Items folder to the Help Document item. - For every
HelpDocument
object, create a detail view. - For every detail view, create a navigation action (ChoiceActionItem) that displays the detail view.
- 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 Help
objects are modified. You can call the Recreate
For more information, refer to the following Git
See Also