tags:

views:

771

answers:

3

Hi, I want to interpret Enter key as Tab key in whole my WPF application, that is, everywhere in my application when user press Enter I want to focus the next focusable control,except when button is focused. Is there any way to do that in application life circle? Can anyone give me an example?
Thanks a lot!

+5  A: 

You can use my EnterKeyTraversal attached property code if you like. Add it to the top-level container on a WPF window and everything inside will treat enter as tab:

<StackPanel my:EnterKeyTraversal.IsEnabled="True">
    ...
</StackPanel>
Matt Hamilton
+1 very cool, nice blog post.
bendewey
A: 

Matt's solution works except I'm having trouble getting the IsEnabled property to change when I set it in the XAML.

It works on the first load where I have a <DockPanel> set to IsEnabled="True" but if I set the property equal to "False" on a specific TextBox or Control where I don't want this behavior to necessarily apply, then its not being recognized.

Any ideas on that one? Should I be able to override the parent elements value on child elements in the XAML?

woodyiii
I discovered to temporarily disable EnterKeyTraversal from being applied on a child element, use the SetIsEnabled method and pass the parent container and a boolean in the GotFocus and LostFocus methods. This worked for my particular problem at least.
woodyiii
A: 

woodyiii, There is a function in the UIElement called PredictFocus() which by its name know its function, then you can check if that element is enabled or not so as to move the focus to it or not...

Sami Abdelgadir Mohammed