Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
PreviewKeyDown/KeyUp not tunnelling
Message
De
05/03/2010 16:08:48
 
 
À
05/03/2010 12:29:14
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01452829
Message ID:
01452867
Vues:
44
>Hi,
>Very simple WPF window:
<Window x:Class="TabTest.Window3"
>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
>    <Canvas x:Name="Outer" Width="500" Height="500" Background="Beige">
>        <ScrollViewer x:Name="SV" Width="500" Height="500" >
>            <Canvas x:Name="Inner" Width="1000" Height="1000" Background="Red" ></Canvas>
>        </ScrollViewer>
>    </Canvas>
></Window>
No Key events (i.e. PreviewKeyDown,KeyDown,PreviewKeyUp,KeyUp) fire on the 'Inner' Canvas.
>The ScrollViewer behaves as I would expect by eating the KeyDown for 'action' keys such as the arrow keys - but I thought the other events would drill down to the inner canvas.
>
>What am I missing .......

In order for the Inner Canvas to receive that event it would have to have the Keyboard focus.

Currently your scroll viewer has the keyboard focus. So PreviewKeyDown originates at the root and travels down the tree to the scroll viewer where it stops and then KeyDown travels back up to the root from the scrollviewer.

Put a textbox on your canvas and as long as the textbox has focus you will see that it now works.

I'm looking... but so far haven't found a way to give a canvas keyboard focus.

Edit: Here is my solution...
XAML:
      <Canvas x:Name="Inner" 
              Focusable="True" 
              Loaded="Inner_Loaded"
              LostFocus="Inner_LostFocus"
              Width="1000" Height="1000" Background="Red">

C#:
    private void Inner_Loaded(object sender, RoutedEventArgs e)
      {
      Inner.Focus();
      }

    private void Inner_LostFocus(object sender, RoutedEventArgs e)
      {
      Inner.Dispatcher.BeginInvoke(DispatcherPriority.Input,
                new ThreadStart(delegate()
                {
                Inner.Focus();
                }));
      e.Handled = true;
      }
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform