The XAML Toolbar element represents a toolbar control in WPF. A ToolBar control is a group of Toolbar button controls that can be clicked to achieve a functionality, forexample the toolbars in Microsoft Word and Visual Studio where you see File Open, Save and Print buttons.
This tutorial explains how WPF Toolbar works, how to add toolbar buttons on a toolbar tray and how to add code to the toolbar button click.
Creating a Toolbar
The ToolBar element in XAML represents a WPF ToolBar control.
The code example in Listing 1 creates a ToolBar control and sets its width and height properties. You may place any control on a ToolBar but typically buttons are used. A ToolBar sits on a ToolBarTray. The code snippet in Listing 1 adds three buttons to the ToolBar and places a ToolBar on a ToolBarTray.
- <ToolBarTray Background="DarkGray" Height="30" VerticalAlignment="Top">
- <ToolBar Name="MyToolbar" Width="200" Height="30" >
- <Button Background="LightSkyBlue" Content="Open" />
- <Button Background="LightSkyBlue" Content="Close" />
- <Button Background="LightSkyBlue" Content="Exit" />
- </ToolBar>
- </ToolBarTray>
Listing 1.
The output looks as in Figure 1.
Figure 1
Adding ToolBar Button Click Event Handlers
The best part of WPF is that these buttons are WPF Button controls so you have a choice to use them on any other button. You may format them the way you like. You may add a click event handler to them and so on.
The code snippet in Listing 2 adds click event handlers to all three ToolBar buttons.
- <ToolBar Name="MyToolbar" Width="200" Height="30" >
- <Button Background="LightSkyBlue" Content="Open" Name="OpenButton" Click="OpenButton_Click" />
- <Button Background="LightSkyBlue" Content="Close" Name="CloseButton" Click="CloseButton_Click" />
- <Button Background="LightSkyBlue" Content="Exit" Name="ExitButton" Click="ExitButton_Click" />
- </ToolBar>
-
Listing 2
On these button click event handlers, you would want to write the code you want to execute when these buttons are clicked. For example, I show a message when these buttons are clicked. The code for these button click event handlers is as in Listing 3.
- private void OpenButton_Click(object sender, RoutedEventArgs e)
- {
- MessageBox.Show("Open button is clicked.");
- }
- private void CloseButton_Click(object sender, RoutedEventArgs e)
- {
- MessageBox.Show("Close button is clicked.");
- }
- private void ExitButton_Click(object sender, RoutedEventArgs e)
- {
- MessageBox.Show("Exit button is clicked.");
- }
Listing 3
If you click on the Open button, you will see Figure 2 as output.
Figure 2
Adding Images to ToolBar Buttons
Usually ToolBars look nicer than just displaying text. In most cases, they have icons. Displaying an Icon image on a button is simply placing an Image control as the content of a Button. The code snippet in Listing 4 changes the Button contents from text to images.
- <ToolBarTray Background="DarkGray" Height="30" VerticalAlignment="Top">
- <ToolBar Name="MyToolbar" Width="200" Height="30" Background="LightCoral" >
- <Button Name="OpenButton" Click="OpenButton_Click">
- <Image Source="Images\camera.png" />
- </Button>
- <Button Name="CloseButton" Click="CloseButton_Click">
- <Image Source="Images\ctv.png" />
- </Button>
- <Button Name="ExitButton" Click="ExitButton_Click" >
- <Image Source="Images\find.png" />
- </Button>
- </ToolBar>
- </ToolBarTray>
Listing 4
The new ToolBar looks as in Figure 3.
Figure 3
Adding Separators to a ToolBar
You may use a Separator control to give your ToolBar buttons a more prominent look. The code snippet in Listing 4 adds a few extra buttons and a few separators to a ToolBar.
- <ToolBarTray Background="DarkGray" Height="30" VerticalAlignment="Top">
- <ToolBar Name="MyToolbar" Width="180" Height="30" Background="LightCoral" >
- <Separator />
- <Button Name="OpenButton" Click="OpenButton_Click">
- <Image Source="Images\camera.png" />
- </Button>
- <Button Name="CloseButton" Click="CloseButton_Click">
- <Image Source="Images\ctv.png" />
- </Button>
- <Button Name="ExitButton" Click="ExitButton_Click" >
- <Image Source="Images\find.png" />
- </Button>
- <Separator Background="Yellow" />
- <Button >
- <Image Source="Images\award.png" />
- </Button>
- <Button >
- <Image Source="Images\cuser.png" />
- </Button>
- <Button >
- <Image Source="Images\next.png" />
- </Button>
- <Button >
- <Image Source="Images\code.png" />
- </Button>
- <Separator />
- </ToolBar>
- </ToolBarTray>
Listing 5
The ToolBar with separators looks as in Figure 4. Also, if you notice there is a drop array that is available when buttons do not fit in a ToolBar. If you click on that, you will see the rest of the buttons.
Figure 4
Summary
In this article, I discussed how to use a ToolBar control in WPF and C#.