WPF Learning Path (v) Example: WordPad

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.