It gives to users a constant visual and easy way to select certain options.
It is mostly used in printing documents or moving pages in an operating system.
WPF Toolbar control enables the end user to re-position the toolbar.
Example
Create a new window, go to XAML Source and write the following code:
[csharp]<Window x:Class="WPFToolbar.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF | Toolbar" Height="350" Width="425">
<Grid>
<ToolBar HorizontalAlignment="Left" VerticalAlignment="Top" Width="100">
<Button Content="File"></Button>
<Button Content="Edit"></Button>
<Button Content="View"></Button>
<Button Content="Project"></Button>
<Button Content="Debug"></Button>
<Button Content="Team"></Button>
<Button Content="Tools"></Button>
</ToolBar>
</Grid>
</Window>[/csharp]
Now run the application.The output appears as below.
If Width is equaled to 100 then the window becomes like below:
If clicked on Toolbar icon then the options shows like below screen:
Toolbars and Commands
Example
Example with Toolbar Commands:
In this example, ToolbarTray control is used at the top of the screen and inside of toolbarTray two Toolbar controls are added.Each one has some buttons and commands are used to see the behavior. In run-time code,CanExecute event of the buttons is used.
Create a new window, go to XAML source and write the following code:
[csharp]
<Window x:Class="WPFToolbar.Commands" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF | Toolbar Commands" Height="300" Width="300">
<StackPanel>
<ToolBarTray>
<ToolBar>
<Button Command="Cut" Content="Cut" />
<Button Command="Copy" Content="Copy" />
<Button Command="Paste" Content="Paste" />
</ToolBar>
</ToolBarTray>
<TextBox Height="50" />
</StackPanel>
</Window>
[/csharp]
Now write the run-time code:
[csharp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WPFToolbar
{
public partial class Commands : Window
{
public Commands()
{
InitializeComponent();
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_CanExecute_2(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
}
}
[/csharp]
Now run the application.The output appears as below.
If written anything in a textbox in the above window, that text can be Cut or Copy.
Before copying or cutting the text, select the text.Notice whether the Copy and Cut options are in enabled/disabled position.
After cutting or copying the text from the textbox, paste the text as per equirement.