How to: Access Navigation Control

This example shows how to access and customize the navigation control used in a WinForms and ASP.NET XAF application. Since customizations will affect only the user interface and will not depend on the current View or data, a Window Controller needs to be created. For detailed information on the navigation system, refer to the Navigation System topic.

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E240.

WinForms Controller

Add a new Window Controller to the WinForms module project and override its protected OnActivated method, subscribe to the ActionBase.CustomizeControl event and perform the required customizations in the event handler.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.XtraNavBar;
using DevExpress.XtraTreeList;
// ...
public class WinCustomizeNavBarController : WindowController {
    protected override void OnActivated() {
        base.OnActivated();
        ShowNavigationItemController navigationController = Frame.GetController<ShowNavigationItemController>();
        if (navigationController != null) {
            Frame.GetController<ShowNavigationItemController>().ShowNavigationItemAction.CustomizeControl += 
ShowNavigationItemAction_CustomizeControl;
        }
    }
    private void ShowNavigationItemAction_CustomizeControl(object sender, 
DevExpress.ExpressApp.Actions.CustomizeControlEventArgs e) {
        NavBarControl navBar = e.Control as NavBarControl;
        if (navBar != null) {
            // Customize NavBar
            foreach (NavBarGroup group in navBar.Groups) {
                if (group.ControlContainer != null && group.ControlContainer.Controls.Count == 1) {
                    TreeList treeList = group.ControlContainer.Controls[0] as TreeList;
                    // Customize embedded TreeList
                }
            }
        }
        else {
            TreeList treeList = e.Control as TreeList;
            if(treeList != null) {
                // Customize TreeList
            }
        }
    }
    protected override void OnDeactivated() {
        ShowNavigationItemController navigationController = Frame.GetController<ShowNavigationItemController>();
        if (navigationController != null) {
            Frame.GetController<ShowNavigationItemController>().ShowNavigationItemAction.CustomizeControl -= 
ShowNavigationItemAction_CustomizeControl;
        } 
        base.OnDeactivated();
    }
}

ASP.NET Controller

Add a new Window Controller to the ASP.NET module project and override its protected OnActivated method to access the current Controller.Frame. Subscribe to the ActionBase.CustomizeControl event and perform the required customizations in the event handler.

Note

The CustomizeControl event cannot be raised if the navigation optimization is enabled (the WebApplication.OptimizationSettings.EnableNavigationControlDelayedCreation property is set to true), because the Navigation Control is not recreated through the callback.

using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Web.Templates.ActionContainers;
//...
public class WebCustomizeNavBarController : WindowController {
    public WebCustomizeNavBarController() {
        TargetWindowType = WindowType.Main;
    }
    protected override void OnActivated() {
        base.OnActivated();
        Frame.GetController<ShowNavigationItemController>().ShowNavigationItemAction.CustomizeControl += 
ShowNavigationItemAction_CustomizeControl;
    }
  private void ShowNavigationItemAction_CustomizeControl(object sender, 
DevExpress.ExpressApp.Actions.CustomizeControlEventArgs e) {
        ASPxNavBar navBar = e.Control as ASPxNavBar;
        if(navBar != null) {
            // Customize the main ASPxNavBar control.
            navBar.EnableAnimation = true;
            foreach(NavBarGroup group in navBar.Groups) {
                foreach(NavBarItem item in group.Items) {
                    if(item is NavBarTreeViewItem) {
                        ASPxTreeView innerTreeView = ((NavBarTreeViewItem)item).TreeViewNavigationControl.Control;
                        // Customize the inner ASPxTreeView control inside the navigation group.
                        innerTreeView.ShowExpandButtons = false;
                    }
                }
            }
        }
        else {
            ASPxTreeView mainTreeView = e.Control as ASPxTreeView;
            if(mainTreeView != null) {
                // Customize the main ASPxTreeView control.
                mainTreeView.ShowExpandButtons = false;
            }
        }
    }
    protected override void OnDeactivated() {
        base.OnDeactivated();
        Frame.GetController<ShowNavigationItemController>().ShowNavigationItemAction.CustomizeControl -= 
ShowNavigationItemAction_CustomizeControl;
    }
}

Mobile Controller

Add a new Window Controller to the Mobile module project and override its protected OnActivated method, subscribe to the ActionBase.CustomizeControl event and perform the required customizations in the event handler.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Mobile.MobileModel;
using DevExpress.ExpressApp.SystemModule;
// ...
public class MobileCustomizeNavigationController : WindowController
{
    protected override void OnActivated()
    {
        base.OnActivated();
        ShowNavigationItemController navigationController = Frame.GetController<ShowNavigationItemController>();
        if (navigationController != null)
        {
            Frame.GetController<ShowNavigationItemController>().ShowNavigationItemAction.CustomizeControl +=
ShowNavigationItemAction_CustomizeControl;
        }
    }
    private void ShowNavigationItemAction_CustomizeControl(object sender,
DevExpress.ExpressApp.Actions.CustomizeControlEventArgs e)
    {
        Navigation navigation = e.Control as Navigation;
        foreach (Command command in navigation.Items)
        {
           // Customize navigation items
        }
    }
    protected override void OnDeactivated()
    {
        ShowNavigationItemController navigationController = Frame.GetController<ShowNavigationItemController>();
        if (navigationController != null)
        {
            Frame.GetController<ShowNavigationItemController>().ShowNavigationItemAction.CustomizeControl -=
ShowNavigationItemAction_CustomizeControl;
        }
        base.OnDeactivated();
    }
}

See Also