none
WPF UserControl OnClose RRS feed

  • Question

  • What is the right way/time to clean things up inside of a WPF user control? I can't seem to find an ControlClosing method or an OnClose method, nor a Dispose.
    Wednesday, April 16, 2008 8:31 PM

Answers

  •  pateramj wrote:

    What is the right way/time to clean things up inside of a WPF user control? I can't seem to find an ControlClosing method or an OnClose method, nor a Dispose.


    Usually, you can subscribe to its Unloaded event to clean up any resources, Unloaded event will be fired up if the UserControl is disconnected from its visual tree, which usually happens when the window (not the MainWindow of the application) containing the UserControl is closed, or you explicitly remove the UserControl from its visual tree, note that if the window is main window, the Unloaded event will not get fired up, basically because when the main window is closed, the whole application is closed, you don't need to clean up resources any more in this circumstance, because the operating system will do it for you when it shuts down process.

    Hope this helps
    Friday, April 18, 2008 7:20 AM
  • I'm doubted about that. Normally, usercontrol is cleaned when it's parent close. What i am doing is creating a public method to clean things inside usercontrol and call it before parent is closing.

     

    Wednesday, April 16, 2008 9:32 PM

All replies

  • I'm doubted about that. Normally, usercontrol is cleaned when it's parent close. What i am doing is creating a public method to clean things inside usercontrol and call it before parent is closing.

     

    Wednesday, April 16, 2008 9:32 PM
  •  pateramj wrote:

    What is the right way/time to clean things up inside of a WPF user control? I can't seem to find an ControlClosing method or an OnClose method, nor a Dispose.


    Usually, you can subscribe to its Unloaded event to clean up any resources, Unloaded event will be fired up if the UserControl is disconnected from its visual tree, which usually happens when the window (not the MainWindow of the application) containing the UserControl is closed, or you explicitly remove the UserControl from its visual tree, note that if the window is main window, the Unloaded event will not get fired up, basically because when the main window is closed, the whole application is closed, you don't need to clean up resources any more in this circumstance, because the operating system will do it for you when it shuts down process.

    Hope this helps
    Friday, April 18, 2008 7:20 AM
  • In fact, i realized that Loaded and Unloaded event just useful when we create usercontrol at design time. If i create usercontrol in code behind, then concat it to other visual tree, Loaded Event fire twice. So i'm going with Initialize event and use public methods to control create/cleanup inside things manually.

     

    Friday, April 18, 2008 7:32 AM
  • Thank you both.
    Friday, April 18, 2008 5:16 PM
  • Marco,

    I have found that when a UserControl is hosted inside of a TabControl and rendered inside of a user-selected tab (tab 1), if the user were to select a different tab (tab 2), then the UserControl object is implicitly unloaded from the visual tree.  That is, the TabControl unloads the UserControl from tab 1 in order to render controls on tab 2.  Can you confirm this? 

    Thank you,

    Dan
    -DK
    Thursday, January 22, 2009 8:22 PM
  • I've also found this. And it looks like it's activating itself again before the user reselects the TabItem. Strange. I'll work out some workaround and report what I find.

    Cheers.
    Wednesday, May 13, 2009 3:26 PM
  • In case anybody cares I've been using the IsVisbibleChanged event to tidy the UserControl presentation around TabItem selections.
    I've also had to implement an IDisposable to manage the clean up when the window ultimately closes, but that's more related to what the UserControl is up to.

    KP

    Wednesday, May 13, 2009 4:04 PM
  • In case anybody cares I've been using the IsVisbibleChanged event to tidy the UserControl presentation around TabItem selections.
    I've also had to implement an IDisposable to manage the clean up when the window ultimately closes, but that's more related to what the UserControl is up to.

    KP

    I don't recommend this. IDisposable is solely for disposing of unmanaged resources. Any use beyond that purpose invites headaches later on. I've had them, they hurt.
    Tuesday, February 8, 2011 7:48 PM
  • There is risk in using a unloaded event because if i have a dockable window where I am using the user control and if I try to undock the window from docked state then unloaded event is fired. But undocking doesnt mean that windows is closed. As you mentioned here that control is being moved from the visual tree. Any idea how to overcome this scenario?
    • Edited by Ash_BLR Friday, July 6, 2012 12:03 PM
    Wednesday, November 9, 2011 9:52 AM
  • I know this is an old thread, but I've just been investigating a similar issue, and have observed this "killer" for the idea of using the Unloaded event to clean up:

    https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.unloaded(v=vs.110).aspx

    Loaded and Unloaded might both be raised on controls as a result of user-initiated system theme changes. A theme change causes an invalidation of the control template and the contained visual tree, which in turn causes the entire control to unload and reload. Therefore Unloaded cannot be assumed to occur only on navigation away from the page.

    Thursday, December 17, 2015 9:04 AM