You'd think you could use MouseEnter and MouseLeave, but when the mouse is captured (as it is during text selection), these events don't fire as expected.
The way to achieve your goal is:
- Subscribe to
MouseMove on the first RichTextBox.
- In the
MouseMove event, check Mouse.Captured to see if it is the RichTextBox.
- If the mouse is captured, do a hit test on the mouse's current postion using
VisualTreeHelper.HitTest. Go up the visual tree from the value of HitTestResult.VisualHit to see if the mouse is over a RichTextBox other than the current one.
- If the mouse is over a new RichTextBox, cancel the mouse capture with
Mouse.Capture(null), then fire a MouseLeftButtonDown event on the new RichTextBox to cause it to capture the mouse and begin selection.