c# - WPF Is possible to map Escape key in theme style -


i have searched , got following code working individual forms.

this.keydown += new keyeventhandler(handleesc);  private void handleesc(object sender, keyeventargs e) {     if (e.key == key.escape)      {          this.close();      } } 

the problem have code same on each , every wpf form in app. looking way put in theme style can avoid repeated coding. there way that?

thanks,

i don't think can done using styles. can though, create new layer between window , window class, , have intermediate layer take care of handling escape key. this, first create new class called escapewindow , have inherit window this:

namespace wpfapplication1 {     public class escapewindow : window     {         public escapewindow()             : base()         {             this.keydown += new keyeventhandler(handleesc);         }          private void handleesc(object sender, keyeventargs e)         {             if (e.key == key.escape) { this.close(); }         }     } } 

now every time create new window, can have inherit escapewindow instead of window. example mainwindow this:

public partial class mainwindow : escapewindow {     public mainwindow()     {         initializecomponent();     } } 

note since wpf windows partially defined in xaml, need change partial implementation. in case, mainwindow.xaml this:

<local:escapewindow x:class="wpfapplication1.mainwindow"          xmlns:local="clr-namespace:wpfapplication1"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">      <grid>     </grid> </local:escapewindow> 

this might not make life easier since need change references every time create window, provides clean framework generically handling escape button.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -