I am creating a form in WPF. Some fields are optional, but they're implemented as such that, their TextBoxes are inside CheckBoxes. Everything seems to be working OK, except that, if the user tries to type in a + sign or a - sign, the checkbox gets checked/unchecked automatically, and the character is not being written to the TextBox.
I would assume that it is because the CheckBox listens to key presses while it or its content have focus, and it may be probably wired up to behave like this by default.
Is there any way in which I can prevent the CheckBox to listen to these 2 keys?
(I am not posting the sample code, because it is just a TextBox inside a CheckBox in WPF)
I figured it out by myself.
So yes, indeed the WPF CheckBox by default listens to key presses on its own KeyDown event handler. This event handler has the peculiarity to do nothing if the CheckBox is a three-state CheckBox, so, it's just a matter of writing some hack-code:
TheCheckBox.IsThreeState = true;
TheCheckBox.Indeterminate += (sender, e) => TheCheckBox.IsChecked = false;
I sincerely don't know why isn't this an option instead of this listener being permanently baked into the CheckBox.