views:

205

answers:

1

Is there any standard way to route all Key events from the control A to other control B? I wish that the keyboard focus will still be on A however the event handler of A would trigger the all event handlers of B for the key events.

edit: Clarification: calling a specific event handler I wrote for B is not enough. I need to mimic the actual event. So for example I want that if a key is sent to a TextBox, it would be written to the TextBox. The solution given below does not do that (not to mention the fact that if new event handlers are added to B it completely fails).

I'm aware that WPF differentiates between logical focus and keyboard focus, but I need both focuses to remain on control A, but in a certain cases route its incoming event to other controls.

+1  A: 

Couldn't you do something like this?

  private void button1_Click(object sender, RoutedEventArgs e)
  {
     // Check if the event needs to be passed to button2's handler
     if (conditionIsMet)
     {
        // Send the event to button2
        button2.RaiseEvent(e);
     }
     else
     {
        // button1's "Click" code
     }
  }

  private void button2_Click(object sender, RoutedEventArgs e)
  {
     // button2's "Click" code
  }

Edit: Modified code to use the RaiseEvent() method to programmatically raise a specific event, rather than just calling the event handler for button2.

Donut
No! That wouldn't trigger the event, it'd just call a single specific function that was once associated with the button's click event. I want to simulate the actual event WPF sends.
Elazar Leibovich
That's what I was looking for!
Elazar Leibovich
I wonder why it's called raise events. I googled for fire event and invoke event and trigger event but with no results...
Elazar Leibovich