Key = Key.Space Then
e.Handled = True
End If
End Sub
You can attach these event handlers to a single text box, or you can wire them up to a
container (such as a StackPanel that contains several numeric-only text boxes) for greater
efficiency.
nNote This key handling behavior may seem unnecessarily awkward (and it is). One of the reasons that
the TextBox doesn??™t provide better key handling is because WPF focuses on data binding, a feature that lets
you wire up controls such as the TextBox to custom objects. When you use this approach, validation is usually
provided by the bound object, errors are signaled by an exception, and bad data triggers an error
message that appears somewhere in the user interface. Unfortunately, there??™s no easy way (at present) to
combine the useful, high-level data binding feature with the lower-level keyboard handling that would be
necessary to prevent the user from typing invalid characters altogether.
Focus
In the Windows world, a user works with one control at a time. The control that is currently
receiving the user??™s key presses is the control that has focus. Sometimes this control is drawn
slightly differently. For example, the WPF button uses blue shading to show that it has the
focus.
In order for a control to be able to accept the focus, its Focusable property must be set to
True.
Pages:
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342