How to: Create a NavBar Control in Code
- 2 minutes to read
This example creates a NavBarControl
in code and adds a Local group with three commands (“Inbox”, “Sent Items”, and “Spam”) to the NavBarControl
.
- Create a
NavBarControl
instance. - Create a NavBarGroup instance and add it to the NavBarControl.Groups collection.
For commands, create NavBarItem objects. Use the ImageOptions property to specify an image for each command. Add the commands to the NavBarGroup.ItemLinks collection.
Tip
You can use Nav
Bar and NavControl. Begin Update Bar methods to prevent excessive updates when multiple properties are modified.Control. End Update - Handle the NavBarControl.LinkClicked event to respond to clicks on commands.
Note
svg
was created and populated at runtime.
using DevExpress.XtraNavBar;
void Form1_Load(object sender, EventArgs e) {
// Create a NavBarControl.
NavBarControl navBar = new NavBarControl();
this.Controls.Add(navBar);
navBar.Dock = DockStyle.Fill;
// Apply the "SkinExplorerBarView" style.
navBar.PaintStyleName = "SkinExplorerBarView";
// Create a Local group.
NavBarGroup groupLocal = new NavBarGroup("Local");
// Create an Inbox item and assign an image.
NavBarItem itemInbox = new NavBarItem("Inbox");
itemInbox.ImageOptions.SvgImage = svgImageCollection1["inbox"];
// Create a disabled Sent Items item.
NavBarItem itemSentItems = new NavBarItem("Sent Items");
itemSentItems.ImageOptions.SvgImage = svgImageCollection1["sent-items"];
itemSentItems.Enabled = false;
// Create a Spam item.
NavBarItem itemSpam = new NavBarItem("Spam");
itemSpam.ImageOptions.SvgImage = svgImageCollection1["spam"];
// Add the items to the group and add the group to the NavBarControl.
// Prevent excessive updates using BeginUpdate and EndUpdate methods.
navBar.BeginUpdate();
navBar.Groups.Add(groupLocal);
groupLocal.ItemLinks.Add(itemInbox);
groupLocal.ItemLinks.Add(itemSentItems);
groupLocal.ItemLinks.Add(itemSpam);
groupLocal.Expanded = true;
navBar.EndUpdate();
// Handle the NavBarControl's LinkClicked event.
navBar.LinkClicked += new NavBarLinkEventHandler(navBar_LinkClicked);
}
void navBar_LinkClicked(object sender, NavBarLinkEventArgs e) {
MessageBox.Show(string.Format("The {0} link has been clicked", e.Link.Caption));
}