Skip to main content

NavLinkCollection.SortByCaption() Method

Sorts the links in the collection by their captions.

Namespace: DevExpress.XtraNavBar

Assembly: DevExpress.XtraNavBar.v24.2.dll

Declaration

public virtual void SortByCaption()

Remarks

This method sorts the links in the collection using the values of their NavBarItemLink.Caption properties, so the links will be arranged in alphabetical order after this method is called.

It is possible to implement custom comparing rules to sort links in a group. For this purpose, create a class implementing the IComparer interface and call the NavLinkCollection.Sort method with an IComparer parameter.

Example

The NavLinkCollection.SortByCaption method allows you to sort a NavBar group’s links by their captions. The following code shows how to maintain sort order when new links are added to groups.

The CollectionChanged event of a group’s NavBarGroup.ItemLinks collection allows you to respond to the link collection changing.

Note: this event is not raised when a link’s caption is changed. So, the sorting routine will not be called in this case.

using DevExpress.XtraNavBar;
//...
//Subscribe to the CollectionChanged events for all the navbar's groups
private void Form1_Load(object sender, System.EventArgs e) {
    for(int i = 0; i < navBarControl1.Groups.Count; i++)
        navBarControl1.Groups[i].ItemLinks.CollectionChanged += 
          new CollectionChangeEventHandler(ItemLinks_CollectionChanged);
}

public void ItemLinks_CollectionChanged(object sender, CollectionChangeEventArgs e) {
    if(e.Action == CollectionChangeAction.Add) {
        NavLinkCollection collection = sender as NavLinkCollection;
        collection.SortByCaption();
    }
}
See Also