Friday, July 3, 2009

How to focus a UserControl on a Key Event in WPF Application

If you want Key Board functionality in your application then this will help you definitely.

If you want focus at the time of creating User Control in WPF application, so that if user trying to use key board functionality directly for shortcuts without touching or focusing the User Control by mouse.......then you have to do these two things.

1. Write this.Loaded += new RoutedEventHandler(UserControl_Loaded) in Contructor.
2. And set these two property in "UserControl_Loaded" function
this.Focusable = true;
this.Focus();


After that, when you run your application you get directly focus on User Control without focusing by mouse and you can use your keyboard for Shortcuts.