Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
PreviewKeyDown/KeyUp not tunnelling
Message
De
06/03/2010 05:54:32
 
 
À
05/03/2010 16:08:48
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:
01452913
Vues:
34
>>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;
>      }
Hi,
Thanks. That explains the behaviour. I'd tried setting Focusable=true but, alone, that didn't help....

But the problem I have with the solution is that the KeyDown on the ScrollViewer doesn't now get handled. Well, I'll re-phrase that - the event does fire and gets marked as handled by the ScrollViewer but it doesn't perform any action (e.g. downarrow doesn't actually scroll down).....

Maybe I need to rethink the approach. All I really need is for the ScrollViewer to handle it's navigation keys normally but for the 'inner' Canvas to handle (a limited number) of other keys.

Any ideas?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform