I am trying the following:
// Key to send
var key = Key.F2;
// Event to send
var routedEvent = Keyboard.KeyDownEvent;
TreeControl.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromVisual(TreeControl),
0,
key) { RoutedEvent = routedEvent }
);
But I can’t simulate F2 programmatically this way though TreeControl has keyboard focus and if I press F2 immediately after manually it works fine. Please help.
I am trying to call this code in the moment OnItemSelected handler is called (not always, when necessary).
Initially posted code is right and I figured out that it simulates F2 press correctly for treeview. Just another problems not related to this prevented me from seeing this.
When I tried to simulate F2 click from
OnItemSelectedhandler it didn’t work but the same fromLayoutUpdatedhandler works fine.So the treeview needs to be fully updated to be ready to accept key presses.