How to fire Unload event of Usercontrol in a WPF window
Asked Answered
D

3

20

I have an UserControl in WPF:

<UserControl x:Class="XLogin.DBLogin"
             x:Name="DBLoginUserFrame"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             Height="263"
             Width="353"
             Loaded="DBLoginUserFrame_Loaded"
             Unloaded="DBLoginUserFrame_Unloaded">
  <Grid>
    <GroupBox Header="Database Connection"
              HorizontalAlignment="Left"
              Height="243"
              Margin="10,10,0,0"
              VerticalAlignment="Top"
              Width="333">
      <Grid>
        <TextBox x:Name="TextUserDB"
                 HorizontalAlignment="Left"
                 Height="20"
                 Margin="101,60,0,0"
                 TextWrapping="Wrap"
                 VerticalAlignment="Top"
                 Width="173" />
        <Label Content="Password:"
               HorizontalAlignment="Left"
               Height="24"
               VerticalAlignment="Top"
               Width="70"
               HorizontalContentAlignment="Right"
               Margin="10,85,0,0" />
        <PasswordBox x:Name="TextPasswordDB"
                     HorizontalAlignment="Left"
                     Height="20"
                     Margin="101,89,0,0"
                     VerticalAlignment="Top"
                     Width="173" />
        <Button x:Name="BtnConnect"
                Content="Connetti"
                Height="29"
                Width="123"
                Margin="101,152,97,24"
                Click="BtnConnect_Click" />
      </Grid>
    </GroupBox>

  </Grid>
</UserControl>

When i Unload this Control, WPF raise event DBLoginUserFrame_Unloaded that save my settings and it does a job.

I have the MainWindow in WPF that load this user control but when window is closed, my usercontrol UNLOAD doesn't fire:

<Window x:Class="XLogin.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:XLogin" Unloaded="Window_Unloaded_1">
<Grid>
    <local:DBLogin/>
</Grid></Window>

How to add the UserControl Unload event to MainWindow event handler?

Dolhenty answered 23/1, 2013 at 11:50 Comment(1)
Check out this work-around. Try "new Window();"Lorca
M
29

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;
}
Maiolica answered 23/1, 2013 at 12:50 Comment(1)
Instead of a user control for a library. I can't trust the consumer of the control to handle this. The Dispatcher.ShutdownStarted solution is perfect for me.Incompetent
P
11

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

Painkiller answered 11/5, 2018 at 11:16 Comment(0)
L
-1

Check out this work-around.

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        new Window(); //<-- this will make Unloaded Event to trigger in WPF
    }
}
Lorca answered 19/7, 2019 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.