How to fire Unload event of Usercontrol in a WPF window

From the documentation:

"Note that the Unloaded event is not raised after an application begins shutting down. Application shutdown occurs when the condition defined by the ShutdownMode property occurs. If you place cleanup code within a handler for the Unloaded event, such as for a Window or a UserControl, it may not be called as expected."

If closing your Window triggers your application's shutdown, that might be the cause. In that case, even the Window's Unload event might not be called as well, so I believe it's better to rely on the Window.Closing event.

One approach to handle the tasks your UserControl does when unloaded is to expose the unload handler method of the control ("DBLoginUserFrame_Unloaded"), name your UserControl instance in the MainWindow and call it from the Window.Closing event.

public MainWindow()
{
    // Add this
    this.Closing += MainWindow_Closing;
}

void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    this.MyUserControl.MethodToBeCalledWhenUnloaded().
}

Another option is to keep your implementation so far but also add in your UserControl a handler for the Dispatcher.ShutdownStarted event, as described here.

public MyUserControl()
{
    this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
}

You could also put the logic into the user control itself to keep it a little more self contained.

public UserControl1()
{
  InitializeComponent();
  this.Loaded += UserControl1_Loaded;
}

void UserControl1_Loaded(object sender, RoutedEventArgs e)
{
  Window window = Window.GetWindow(this);
  window.Closing += window_Closing;
}

void window_Closing(object sender, global::System.ComponentModel.CancelEventArgs e)
{
 //Save your settings here
}

Saw this here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/477e7e74-ccbf-4498-8ab9-ca2f3b836597/how-to-know-when-a-wpf-usercontrol-is-closing?forum=wpf