I have a MyToggleButton user control that inherits from System.Windows.Controls.Primitives.ToggleButton.
If keyboard focus is on another control, and I left-mouse click the MyToggleButton, the keyboard focus is transferred to the MyToggleButton control.
Is there a way that I can prevent the keyboard focus transfer if the MyToggleButton is left-mouse clicked?
NOTE: the user must still be able to Tab to the MyToggleButton control, which would set the keyboard focus to it. I just don't want the user to be able to set keyboard focus to it via a left-mouse click.
Again, the control needs to be clickable via a left-mouse click. I DO NOT want the control to have keyboard focus after it is clicked.
You can make your control insensitive to mouse input by setting
HitTestVisible
to false or by consuming thePreviewMouseLeftButtonDown
event and marking it as handled. Both ways have the side effect that mouse interaction is partially or fully inhibited for the control. Using the event handler you still can do right-clicks. Keyboard nav and input however is not effected and still functional.Here is an example how it works with Textboxes. You can step through all 4 textboxes with TAB, but you can not select the 2nd and 3rd with the left mouse, the 3rd however is responsive to right-mousebutton-clicks. The focused textbox always appears in lightly red background for demo purpose.
And that's the eventhandler (stub):