In this part of my WPF tutorial I’ll cover how to do a ton of things with toolbars. We’ll Create the Toolbar, Add Icons, Open / Save Files, Use Shortcuts, Copy / Cut / Paste, Change Fonts in Comboboxes and more.
For best results take notes on the cheat sheet provided below as you watch and leave any questions you have.
If you value videos like this consider donating a $1 on Patreon.
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
// ---------- WpfTutorial ---------- // ---------- MainWindow.xaml.cs ---------- using Microsoft.Win32; 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.Navigation; using System.Windows.Shapes; namespace WpfTutorial { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void MenuExit_Click(object sender, RoutedEventArgs e) { this.Close(); } private void MenuOpen_Click(object sender, RoutedEventArgs e) { OpenFileDialog openDlg = new OpenFileDialog(); openDlg.ShowDialog(); } private void MenuSave_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.ShowDialog(); } private void MenuFontTimes_Click(object sender, RoutedEventArgs e) { menuFontCourier.IsChecked = false; menuFontArial.IsChecked = false; txtBoxDoc.FontFamily = new FontFamily("Times New Roman"); } private void MenuFontCourier_Click(object sender, RoutedEventArgs e) { menuFontTimes.IsChecked = false; menuFontArial.IsChecked = false; txtBoxDoc.FontFamily = new FontFamily("Courier"); } private void MenuFontArial_Click(object sender, RoutedEventArgs e) { menuFontCourier.IsChecked = false; menuFontTimes.IsChecked = false; txtBoxDoc.FontFamily = new FontFamily("Arial"); } // ---------- NEW STUFF ---------- // Used to track if font size combobox is closed private bool comboFSClosed = true; // Change font size of text box after combo is closed private void ComboFontSize_DropDownClosed(object sender, EventArgs e) { if (comboFSClosed) ChangeTBFontSize(); comboFSClosed = true; } // Verify combo is closed and call for font size change private void ComboFontSize_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBox combobox = sender as ComboBox; comboFSClosed = !combobox.IsDropDownOpen; ChangeTBFontSize(); } private void ChangeTBFontSize() { // Convert combo font size data to string string fontsize = comboFontSize.SelectedItem.ToString(); // Get the last 2 numbers fontsize = fontsize.Substring(fontsize.Length - 2); switch (fontsize) { case "10": txtBoxDoc.FontSize = 10; break; case "12": txtBoxDoc.FontSize = 12; break; case "14": txtBoxDoc.FontSize = 14; break; case "16": txtBoxDoc.FontSize = 16; break; } } } } // ---------- MainWindow.xaml ---------- <Window x:Class="WpfTutorial.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfTutorial" xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" SizeToContent="WidthAndHeight" Topmost="False" WindowState="Normal" Icon="./Resources/favicon.ico"> <DockPanel> <Menu DockPanel.Dock="Top"> <MenuItem Header="_File"> <MenuItem x:Name="menuNew" Header="_New" InputGestureText="Ctrl+N"> <MenuItem.Icon> <Image Source="./Resources/New.bmp"/> </MenuItem.Icon> </MenuItem> <MenuItem x:Name="menuOpen" Header="_Open" InputGestureText="Ctrl+O" Click="MenuOpen_Click"> <MenuItem.Icon> <Image Source="./Resources/Open.bmp"/> </MenuItem.Icon> </MenuItem> <MenuItem x:Name="menuSave" Header="_Save" InputGestureText="Ctrl+S" Click="MenuSave_Click"> <MenuItem.Icon> <Image Source="./Resources/Save.bmp"/> </MenuItem.Icon> </MenuItem> <MenuItem x:Name="menuExit" Header="Exit" Click="MenuExit_Click"> </MenuItem> </MenuItem> <MenuItem Header="_Edit"> <MenuItem x:Name="menuCut" Header="Cut" Command="ApplicationCommands.Cut" InputGestureText="Ctrl+X"/> <MenuItem x:Name="menuCopy" Header="Copy" Command="ApplicationCommands.Copy" InputGestureText="Ctrl+C"/> <MenuItem x:Name="menuPaste" Header="Paste" Command="ApplicationCommands.Paste" InputGestureText="Ctrl+V"/> <Separator /> <MenuItem Header="_Font" InputGestureText="Ctrl+F"> <MenuItem x:Name="menuFontTimes" Header="Times" IsCheckable="True" StaysOpenOnClick="True" Click="MenuFontTimes_Click"/> <MenuItem x:Name="menuFontCourier" Header="Courier" IsCheckable="True" StaysOpenOnClick="True" Click="MenuFontCourier_Click"/> <MenuItem x:Name="menuFontArial" Header="Arial" IsCheckable="True" StaysOpenOnClick="True" Click="MenuFontArial_Click"/> </MenuItem> </MenuItem> </Menu> <!-- ---------- NEW STUFF ---------- --> <!-- A toolbar provides quick access to tools --> <!-- You can also dock on the left or right DockPanel.Dock="Right" Orientation="Vertical" --> <ToolBarTray DockPanel.Dock="Top"> <ToolBar> <Button x:Name="tbOpen" ToolTip="Open File" Click="MenuOpen_Click"> <Image Source="./Resources/Open.bmp"></Image> </Button> <Button x:Name="tbSave" ToolTip="Save File" Click="MenuSave_Click"> <Image Source="./Resources/Save.bmp"></Image> </Button> <Button x:Name="tbCut" Command="ApplicationCommands.Cut"> <Image Source="./Resources/Cut.bmp"></Image> </Button> <Button x:Name="tbCopy" Command="ApplicationCommands.Copy"> <Image Source="./Resources/Copy.bmp"></Image> </Button> <Button x:Name="tbPaste" Command="ApplicationCommands.Paste"> <Image Source="./Resources/Paste.bmp"></Image> </Button> <Separator /> <Label>Font size:</Label> <ComboBox x:Name="comboFontSize" SelectionChanged="ComboFontSize_SelectionChanged" DropDownClosed="ComboFontSize_DropDownClosed"> <ComboBoxItem>10</ComboBoxItem> <ComboBoxItem IsSelected="True">12</ComboBoxItem> <ComboBoxItem>14</ComboBoxItem> <ComboBoxItem>16</ComboBoxItem> </ComboBox> </ToolBar> </ToolBarTray> <TextBox x:Name="txtBoxDoc" Height="290" Width="500" FontSize="12"/> </DockPanel> </Window> |
Leave a Reply