WPF learning path (5) Example: wordboard, wpf path
Wordpad instance 1
MainWindow. xaml
<Window x:Class="Wordpad01.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WordPad1.0" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Menu Grid.Row="0"> <MenuItem Header="File" /> <MenuItem Header="Copy" /> <MenuItem Header="Paste" /> <MenuItem Header="Cut" /> <MenuItem Header="Delete" /> </Menu> <ToolBar Grid.Row="1"> <Button> <Image Source="/Images/Copy.png" /> </Button> <Button> <Image Source="/Images/Paste.png" /> </Button> <Button> <Image Source="/Images/Cut.png" /> </Button> <Button> <Image Source="/Images/Delete.png" /> </Button> </ToolBar> <TextBox x:Name="text" Grid.Row="2" Text="WordPad" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid></Window>
Mainly relies on Clipboard class implementation
Add the Click Time for the Button
<MenuItem Header = "Copy" Click = "CopyClick"/>
<Button Click = "CopyClick">
private void CopyClick(object sender, RoutedEventArgs e){ if (text.Text != null && text.Text.Length > 0) { Clipboard.SetText(text.Text); }}private void PasteClick(object sender, RoutedEventArgs e){ if (Clipboard.ContainsText()) { text.Text = Clipboard.GetText(); }}private void CutClick(object sender, RoutedEventArgs e){ CopyClick(sender, e); DeleteClick(sender, e);}private void DeleteClick(object sender, RoutedEventArgs e){ text.Text = null;}
Right-click menu
<TextBox x:Name="text" Grid.Row="2" Text="WordPad" FontSize="30" TextWrapping="Wrap" Height="Auto" Width="Auto" Margin="5"> <TextBox.ContextMenu> <ContextMenu> <MenuItem Header="Copy" Click="CopyClick" /> <MenuItem Header="Paste" Click="PasteClick" /> <MenuItem Header="Cut" Click="CutClick" /> <MenuItem Header="Delete" Click="DeleteClick" /> </ContextMenu> </TextBox.ContextMenu>
</TextBox>
Add the shortcut key KeyGesture
private KeyGesture gestCopy = new KeyGesture(Key.C, ModifierKeys.Control);private KeyGesture gestPaste = new KeyGesture(Key.V, ModifierKeys.Control);private KeyGesture gestCut = new KeyGesture(Key.X, ModifierKeys.Control);private KeyGesture gestDelete= new KeyGesture(Key.Delete);protected override void OnPreviewKeyDown(KeyEventArgs e){ base.OnPreviewKeyDown(e); e.Handled = true; if (gestCopy.Matches(null, e)) { CopyClick(this, e); } else if (gestPaste.Matches(null, e)) { PasteClick(this, e); } else if (gestCut.Matches(null, e)) { CutClick(this, e); } else if (gestDelete.Matches(null, e)) { DeleteClick(this, e); }}
Add Status Control
Menu Bar
<MenuItem Header="File" SubmenuOpened="MenuItem_SubmenuOpened"/><MenuItem x:Name="copyItem" Header="Copy" Click="CopyClick" /><MenuItem x:Name="pasteItem" Header="Paste" Click="PasteClick" /><MenuItem x:Name="cutItem" Header="Cut" Click="CutClick" /><MenuItem x:Name="deleteItem" Header="Delete" Click="DeleteClick" />
private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e){ cutItem.IsEnabled = copyItem.IsEnabled = deleteItem.IsEnabled = text.Text != null && text.Text.Length > 0; pasteItem.IsEnabled = Clipboard.ContainsText();
}
Similarly, TextBox
<TextBox x: Name = "text" ContextMenuOpening = "text_ContextMenuOpening">
The shortcut key is the same as the toolbar.
A simple WordPad program has been completed, but is it difficult to find it? In the next issue, we will update WordPad2.0.
To be continue...