Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Thursday, June 28, 2012

Dialogs in WPF

OK and Cancel Buttons in a Dialog

You have a modal dialog with several buttons on it and you want to automatically close it, when the user presses on some of them. To do this you have to set IsCancel="true" on all buttons that should close the dialog and return false. On one button you set IsDefault="true" this will be executed when you press [Enter]. It closes the dialog and returns... also false. To return true here you have to register a callback that sets the DialogResult to true
 
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Button Content="Cancel" IsCancel="True" />
        <Button Click="OkClick" Content="Ok" IsDefault="true" />
    </StackPanel>
</Window>
 
 
 
private void OkClick(object sender, RoutedEventArgs e)  
{
    this.DialogResult = true;  
}
 

No comments:

Post a Comment