Adventures with Windows Presentation Foundation Part I

I am finally biting the bullet and learning something about WPF.  I have Visual Studio 2008 and Expression Blend installed and I am off to the races.  I have been doing Windows programming since Windows 3.0 and followed the evolution of UI development using MFC, VB, and WinForms.   WPF is very cool.   I read this by David Chappell when I first got started with WPF.  It is a good read.

So, last night I was working away on one of my prototype projects and I needed to pop a dialog to collection some information from the user.  Well, this is my first time popping a dialog in WPF and I encountered several differences between WinForms and WPF dialogs.

I have a control hosted in a Window.  The control is responsible for popping the dialog to gather additional information.  I would like the dialog to pop centered over the parent window.  I would like to know if the user chose to cancel the dialog or not.   Here is the source:

    1: ProfileManagerWindow profileManagerWindow = new ProfileManagerWindow
    2:                                                 {
    3:                                                     Owner = Window.GetWindow(Parent),
    4:                                                     ShowInTaskbar = false,
    5:                                                     WindowStartupLocation =
    6:                                                         WindowStartupLocation.CenterOwner
    7:                                                 };
    8:  
    9: profileManagerWindow.ShowDialog();
   10:  
   11: if (!profileManagerWindow.DialogResult.HasValue || !profileManagerWindow.DialogResult.Value)
   12: {
   13:     return;
   14: }

A few things to notice:

  1. I set the owner by calling a static method on the Window class called GetWindow.  I pass the control's parent to method, which in turn returns the Window object associated with Parent's FrameworkElement
  2. I call ShowDialog, but unlike WinForms, I don't collect the DialogResult as a return value
  3. DialogResult is a bool? type, so if it is null or is set to false, I know that the user canceled the dialog.

Very different from WinForms, but that's the point!