In this part of my C# tutorial we’ll continue learning about WPF and XAML. We’ll cover the MenuBars, Assigning Shortcuts, Grid Layout, ScrollViewer, DockPanel, Changing Fonts, the OpenFileDialog, the SaveFileDialog and a whole lot more.
For best results take notes on the cheat sheet provided above as you watch and leave any questions you have.
If you like 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 |
// ---------- WpfTutorial ---------- // ---------- 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"> <!-- Create a menu bar Put _ before the shortcut key which is triggered with Alt - Whatever --> <!-- 1. Get the Visual Studio Image Library if you want standard icons A. https://www.microsoft.com/en-us/download/details.aspx?id=35825 B. I got VS2013 Image Library.zip --> <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> <Separator /> <MenuItem x:Name="menuExit" Header="Exit" InputGestureText="Ctrl+F4" Click="MenuExit_Click"/> </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> <TextBox x:Name="txtBoxDoc" Height="290" Width="500"/> </DockPanel> <!-- You can create rows and columns in the Grid layout by just clicking the borders --> <!-- You can drag a label in a cell, copy and paste some more while changing the margins select them and Group Into -> ScrollViewer <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="153*"/> <ColumnDefinition Width="208*"/> <ColumnDefinition Width="156*"/> </Grid.ColumnDefinitions> <ScrollViewer Grid.Column="2" Margin="0,0,0,193"> <Grid> <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,-17,0"/> <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,25,-17,0"/> <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,50,-17,0"/> <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,75,-17,0"/> <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,100,-17,0"/> </Grid> </ScrollViewer> </Grid> --> <!-- A DockPanel is normally used as a container for other panels <DockPanel> <Label x:Name="lblTop" DockPanel.Dock="Top" Content="TOP"/> <Label x:Name="lblLeft" DockPanel.Dock="Left" Content="LEFT"/> <Label x:Name="lblRight" DockPanel.Dock="Right" Content="RIGHT"/> <Label x:Name="lblBottom" DockPanel.Dock="Bottom" Content="BOTTOM"/> <Label x:Name="lblCenter" Content="CENTER"/> </DockPanel> --> </Window> // ---------- 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(); } // Closes the app private void MenuExit_Click(object sender, RoutedEventArgs e) { this.Close(); } // Opens the open dialog private void MenuOpen_Click(object sender, RoutedEventArgs e) { OpenFileDialog openDlg = new OpenFileDialog(); openDlg.ShowDialog(); } // Opens the save dialog private void MenuSave_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.ShowDialog(); } // Unchecks other fonts and changes font for the text box 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"); } } } |
Leave a Reply