Behaviours are a great way to encapsulate useful functionality in a self-contained object. These objects can be attached to XAML elements in a Silverlight page to enable features that the elements do not themselves contain or know about.
A feature present in Win Forms and missing from Silverlight is the concept of Default Submit and Cancel Buttons. That is: a button that is clicked automatically when Enter or Escape is pressed while entering text in TextBox controls on a form.
Type text into any of the 3 TextBox controls below and press the Enter key to “submit” the form or the Escape key to “cancel” the form. For test purposes random numbers appear under the buttons to show that the button actions have been fired (you can just press the Go or Cancel buttons directly to see this).
The example above uses our new DefaultButtonBehavior and CancelButtonBehaviour. The features are:
- Can be attached to single text boxes, or to entire forms.
- Can be enabled independently of the text boxes (this property is bindable).
- Can ignore empty text boxes (this property is bindable). This is useful for single text box user controls, like a search box.
- Automatically presses and releases the target button, based on the Enter or Escape key press and release, to give good visual feedback.
We have released this behaviour to the Expression Gallery. Feel free to download and try it out. Feedback and suggestions welcomed!