2
votes

I have another issue in converting my Winforms program to a WPF program. In my first program, I had a smaller window open to allow the user to adjust some data, and then when it closed, the other form was activated again with the new data.

I used form2.ShowDialog(); to open the form, which automatically makes the parent form deactivated in Winforms. This way when I closed form2, the parent form was activated, and I was able to use an event handler form1_Activated to reload and re-initialize some of the settings successfully.

However, now when I attempt to do the same thing with WPF, I am still able to open form2 using form2.ShowDialog();, but then when I close the form, it does not register the form1_Activated event handler. Instead, in order to reload the settings, I must click on another window, and then come back into my program to register the form1_Activated event handler.

Am I just doing something wrong, or is there another event handler that I should be using in WPF to achieve the same thing I was able to do in Winforms?

1
Window activation is pretty flaky in WPF. Then again, you already did it the wrong way in Winforms. Move your code after the ShowDialog() call. And don't forget to pay attention to its return value. - Hans Passant
@HansPassant then is there another method that I can use to trigger an event in the first window once the second window is closed like I was able to do in Winforms? - Jacob Varner
You can raise your own event after the ShowDialog() call. Do note the absurdity of this, there's no point in a class listening to its own events. Just call the event handler directly. - Hans Passant
@HansPassant your approach is absurd because UI is not Data. Applying the right patterns in WPF removes the need for absurd stuff, and code behind in general. Refer to my answer. - Federico Berasategui

1 Answers

1
votes

Calling ShowDialog() causes the dialog box top appear in modal mode so I don't understand why you would need an event handler to process the results after the dialog box is closed. Keep in mind that you can access public variables in the DialogBox, as well. If I understand your question, this should do what you are asking:

MainWindow:

My_DialogBox dlg = new My_DialogBox();
dlg.Owner = this;
dlg.MyPublicVariable = ''; //some value that you might need to pass to the dialog
dlg.ShowDialog();  //exection of MainWindow is suspended until dialog box is closed

if (dlg.DialogResult == true)
{
    //dlg.MyPublicVariable is still accessible 
    //call whatever routines you need in order to refresh the main form's data
}

DialogBox:

private void OK_Button_Click(object sender, RoutedEventArgs e)
{
    MyPublic variable = something;  //accessible after the dialog has closed.
    this.DialogResult = true;
}

private void Cancel_Button_Click(object sender, RoutedEventArgs e)
{
     this.DialogResult = false;
}

The MSDN write-up on dialog boxes is pretty good. There may be some tips that might help you even more: http://msdn.microsoft.com/en-us/library/aa969773.aspx

Good luck!