ToolBar. Programs often have buttons that are easy to access. These are stored in ToolBars. We arrange ToolBars in a ToolBarTray. We can add nested controls to our ToolBar.
Getting started. A ToolBarTray is used to contain ToolBars—so we should add it first. Please drag a ToolBarTray from the Toolbox to your window Grid.
Example code. Here I add 2 ToolBars to the ToolBarTray. In the first ToolBar, I add 2 Buttons—a ToolBar can store Buttons and Images and other controls.
Click. Here we add some code to the contents of Button_Click. When you run it, this code will help you see how the Band and BandIndex properties change as ToolBars are dragged.
Tip The BandIndex is the horizontal index, while the Band is the vertical index. This program makes this concept clear.
using System.Windows;
namespace WpfApplication11
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// ... Change the title when this button is clicked.
this.Title = "Band: " + this.Bar1.Band.ToString() +
"; BandIndex: " + this.Bar1.BandIndex.ToString();
}
}
}
Testing. In testing ToolBars, try dragging the ToolBars around. You should be able to position them on the same band, next to each other.
Summary. Programs can have complex UIs, with lots of buttons. The ToolBar control, and its companion ToolBarPanel, makes it simpler to lay out these buttons.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 29, 2022 (edit).