Tip to enable key-events in WPF

Working on a project with editing shapes I came over a problem when I wanted to canced the operation when the user pressed the escape-key. When I added an event handler for KeyPress or KeyDown or even PreviewKeyDown it never fired.

This was because the graphical elements didn't actully have focus.

For my user-control for the graphic element I solved this by adding this in the constructor:

this.Focusable = true;
this.FocusVisualStyle = null;

The second line ensured that the dotted focus-rectangle also disseapeared.

For a xaml-based approach this is the markup:

<Canvas Focusable="True" FocusVisualStyle="{x:Null}">
 <!-- The shapes or elements like an elipse, rectangle, path or stack-panel... --> 
</Canvas>




Comments

Popular posts from this blog

Custom Pushpin in Bing Maps Control for WPF

Bing Maps Control for WPF with Essential Tools