WordPad instance One
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=" -"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=" -"Horizontalalignment="Center"Verticalalignment="Center"/> </Grid></Window>
Mainly rely on the Clipboard class to achieve
Add Click Time for button
<menuitem header= "Copy" click= "Copyclick"/>
<button click= "Copyclick" >
Private voidCopyclick (Objectsender, RoutedEventArgs e) { if(text. Text! =NULL&& text. Text.length >0{clipboard.settext (text). Text); }}Private voidPasteclick (Objectsender, RoutedEventArgs e) { if(Clipboard.containstext ()) {text. Text=Clipboard.gettext (); }}Private voidCutclick (Objectsender, RoutedEventArgs e) {Copyclick (sender, E); Deleteclick (sender, E);}Private voidDeleteclick (Objectsender, RoutedEventArgs e) {text. Text=NULL;}
Add Right-click menu
<textbox x:name="text"grid.row="2"text="WordPad"Fontsize=" -"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 shortcut keys KeyGesture
PrivateKeyGesture gestcopy =NewKeyGesture (key.c, Modifierkeys.control);PrivateKeyGesture Gestpaste =NewKeyGesture (KEY.V, Modifierkeys.control);PrivateKeyGesture Gestcut =NewKeyGesture (key.x, Modifierkeys.control);PrivateKeyGesture gestdelete=NewKeyGesture (key.delete);protected Override voidOnpreviewkeydown (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 State 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= copyitem.isenabled = deleteitem.isenabled null 0= clipboard.containstext ();
}
TextBox in the same vein
<textbox x:name= "text" contextmenuopening= "text_contextmenuopening" >
Shortcut keys and toolbars above
A simple WordPad program completed, but there is no trouble to find, the next edition of update WordPad2.0
To be continue ...
WPF Learning Path (v) Example: WordPad