#848 – IsDefaulted vs. IsDefault for a Button

The IsDefault property can be set for a Button to indicate that you’d like the button to act as a default button for a window, i.e. activate when the user presses the ENTER key.

The IsDefaulted property is a read-only property that indicates if a Button is currently acting as the default button for a window, meaning that it would activate if the user pressed ENTER at this moment.

IsDefaulted will be true if all of the following is true:

  • IsDefault for the Button is set to true
  • Some other control currently has focus
  • The control that currently has focus does not itself handle the ENTER key



#847 – Default Button Behavior Depends on Focus

When you set the IsDefault property of a Button to true, the user can activate the button by pressing the ENTER key.

The default behavior of a Button, however, will depend on which control in the window has focus when the user presses ENTER.  If the control that has focus can itself handle the ENTER key, that control will activate when ENTER is pressed, rather than a Button that has IsDefault set to true.

In the example below, the second TextBox has AcceptsReturn set to true and the “Save” button has IsDefault set to true.  Because both the second TextBox and the Cancel button can themselves handle the ENTER key, the “Save” button will only activate on ENTER if the first TextBox has focus when ENTER is pressed.



#315 – Setting IsDefault and IsCancel to the Same Button

You normally set either the IsDefault or IsCancel properties on a button, depending on its purpose.  But you can also set both properties to true for the same button.  You’d typically do this on a dialog that only has one button for closing the dialog.  In this case, you’d want the button to activate when the user pressed either ENTER or ESC (Escape).

	            <Button Content="OK" Width="100" Margin="15"
	                    IsDefault="True" IsCancel="True"

#314 – Typical Usage for Default and Cancel Buttons

You’ll typically see Default and Cancel buttons used together in a window or dialog, to provide OK / Cancel functionality.

The idea is that if the user presses the ENTER key, the button marked with the IsDefault property receives a Click event, as if the user had clicked on the OK button.  The data that the user entered in the dialog is accepted and the window disappears.

If the user presses the ESC (Escape) button, the button marked with the IsCancel property receives a Click event, as if the user had clicked on the Cancel button.  The data that the user entered in the dialog is abandoned (not saved) and the window disappears.

#312 – Specify a Default Button in a Window

You can use the IsDefault property to specify that a particular button in a window is the default button.  You can still click on the button like you would any other button in the window, but if you press the ENTER key, the default button will respond as if it was clicked.  The button responds to the ENTER key, no matter which control currently has keyboard focus.

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Content="Save" Width="100" Margin="10,20,10,20"
            <Button Content="Cancel" Width="100" Margin="10,20,10,20"