7

I want to set focus to the ui element(ui element is popup which contains devexpress grid) when I press down arrow key in the text box. I would like it to work purely on XAML side or by using MVVM. will this work. pls let me know the way i can accomplish this.

<KeyBinding Key="Down" FocusManager.FocusedElement="{Binding ElementName=PART_GridControl}"/>

i was using code behind like this but i want to implement mvvm.i want to change the focus from text box to the grid control, when i press down arrow key in text box –

 private void text_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.Key == Key.Down || e.Key == Key.Up || e.Key == Key.Left || e.Key == Key.Right)
     {
        PART_GridControl.Focus();
     }      
 }
dowhilefor
  • 10,971
  • 3
  • 28
  • 45
user1379584
  • 151
  • 2
  • 4
  • 10
  • is not working – user1379584 May 21 '12 at 12:18
  • Perhaps I'm confused, but wouldn't the fact of getting a keydown event in the textbox already mean it has focus? – Joel Lucsy May 21 '12 at 13:11
  • i want to change the focus from text box to the grid control, when i press down arrow key in text box – user1379584 May 21 '12 at 13:15
  • What you want is to move the focus from one element to the next, Joel Lucsy had the right idea, you can't handle the keydown event when you don't have the focus. Also, you can't set the focus if the element is not focusable. Checkout [KeyboardNavigation](http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardnavigation.aspx) – dowhilefor May 21 '12 at 13:27
  • http://stackoverflow.com/questions/1356045/set-focus-on-textbox-in-wpf-from-view-model-c-wpf/1356781#1356781 This link has the answer and it works too – user1379584 May 24 '12 at 06:05
  • this may be a help for you http://stackoverflow.com/questions/35228434/setting-focus-to-ui-control-in-wpf-mvvm-way-on-validation-haserror/35228483#35228483 – WPFKK Feb 05 '16 at 16:08

0 Answers0