prabhakar
prabhakar

Reputation: 57

how to focus the cursor in the current cell in datagrid in wpf

i am doing validation in the celleditending event in the datagrid. once the message is displayed in the message box if i click ok in the message box .the focus in the datagrid is moved to the previous row. i want the focus in the current validating row.

i tried this code but it did'nt worked.

int index = attendancegrid .SelectedIndex;
 attendancegrid .SelectedItem = attendancegrid .Items[index];
 attendancegrid .ScrollIntoView(attendancegrid .Items[index]); 

if anyone have idea about how to focus the current validating row help me with some code ex

Upvotes: 0

Views: 648

Answers (1)

dev hedgehog
dev hedgehog

Reputation: 8791

Whenever you display a MessageBox and you click on OK you lose focus from your MainWindow. Though logical focus is being saved so when you return back to MainWindow, you see the logical focus which can be turned into keyboad focus when clicking back inside the scope.

To read more about that check this link out.

http://msdn.microsoft.com/en-us/library/aa969768.aspx

If you wish to use logical focus and let the wpf do the job for you, then read this:

http://msdn.microsoft.com/en-us/library/System.Windows.Input.FocusManager(v=vs.110).aspx

Now to your question. I would solve this by using FocusManager.FocusedElement. Just set that property inside your error template.

An alternative would be to subscribe to Validation.Error event and force Keyboard.Focus method to focus object sender inside the event handler.

http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.error.aspx

Upvotes: 1

Related Questions