Has anyone else noticed that handling keypresses in a WPF TextBox is inordinately awkard? For example, if you want to build a numbers-only textbox, PreviewKeyDown not's much help--it's so low level that it distinguishes between things like Key.D9 and Key.NumPad9 (two versions of the "9" character). You could use the KeyConverter to try to streamline this, but that runs into problems when you encounter something like the backspace key (which becomes the text "Backspace"). PreviewTextInput lets you get the actual character (no low-level value from the Key enumeration), but it only fires for some keys, so you're out of luck if you want to rule out the space key, for example.
The best solution I've found so far is to handle both, PreviewTextInput for the easy stuff and PreviewKeyDown for everything else (special keys). But this is still ugly. I know in a completely modern straight-from-the-sample-page WPF application everything would be hooked up through data bindings and we'd have to make do with a validation message that flags incorrect values after they've been entered. But assuming you really do want to make invalid input impossible (and there are good reasons to do this on occasion), it's unfortunate that the WPF TextBox makes it so difficult. Or are there a few tricks I'm missing? Matthew =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com