views:

50

answers:

1

The book says

The WPF Button class only adds two simple concepts on top of what ButtonBase already provides: being a cancel button or a default button. These two mechanisms are handy short- cuts for dialogs. If Button.IsCancel is setto true on a Button inside a dialog (that is, a Window shown via its ShowDialog method), the Window is automatically closed with a DialogResult of false. If Button.IsDefault is set to true, pressing Enter causes the Button to be clicked unless focus is explicitly taken away from it.

But in this sample window

<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
Background="OrangeRed" >
    <StackPanel>
        <Label  FontWeight="Bold" FontSize="20" Foreground="White">
            WPF Unleashed (Version 3.0)
        </Label>
        <Label> 2006 SAMS Publishing</Label>
        <Label>Installed Chapters:</Label>
        <ListBox>
            <ListBoxItem>Chapter 1</ListBoxItem>
            <ListBoxItem>Chapter 2</ListBoxItem>
        </ListBox>
        <TextBox AcceptsReturn="False">HELLO TEXT</TextBox>
        <RadioButton>HELLO RADIO BUTTON</RadioButton>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button IsCancel="True" MinWidth="75" Margin="10">Cancel</Button>
            <Button x:Name="OKBUTTON" IsDefault="True" MinWidth="75" Margin="10">OK</Button>
        </StackPanel>
        <StatusBar>You have successfully registered this product.</StatusBar>
    </StackPanel>
</Window>

If I press Enter or even click it, the modal Window(By ShowDialog()) does not get closed (leave aside the return value). Is that an error in the book ?

+1  A: 

I believe it is an error. IsDefault and IsCancel simply means that some access-key-magic is applied when the window is created, so that the button is clicked when you hit 'Enter' and 'ESC' respectively.

If you want a Window to close - you need to: (from MSDN ) When a dialog box is accepted, it should return a dialog box result of true, which is achieved by setting the DialogResult property when the OK button is clicked ... Note that setting the DialogResult property also causes the window to close automatically, which alleviates the need to explicitly call Close.

Goblin