0

I'm currently working on a WPF project using Caliburn.Micro and want to bind a KeyDown event to a UserControl. It should be fired if the Window is opened and the user pushes any button.

<UserControl x:Class="Test.Views.AppView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:cal="http://www.caliburnproject.org"
         cal:Message.Attach="[Event KeyDown] = [TestMethod($executionContext)]" />

Unfortunately this code doesn't work. Is it even possible to bind an Event to the UserControl and not to specific controls like TextBox or Button?

Vimal CK
  • 3,543
  • 1
  • 26
  • 47
  • see if this link helps you - http://stackoverflow.com/questions/16719496/caliburn-micro-enter-key-event – Vinkal Feb 06 '15 at 09:17

1 Answers1

1

If a control on this UserControl has focus it will receive the KeyDown event first and handle it. This will prevent the UserControl from receiving it.

Use PreviewKeyDown to capture the event. The Preview... events are exactly for this kind of scenario. They bubble up from the root to the child controls whereas the regular events tunnel down.

Don't forget to set e.Handled = true; at the end of a handler if the bubbling and tunneling should stop.

Emond
  • 50,210
  • 11
  • 84
  • 115