BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Base Classes - FrameworkElement - Object Lifetime

The twenty-sixth part of the Windows Presentation Foundation Fundamentals tutorial continues to look at the members of the FrameworkElement type. This article describes three object lifetime events that can be captured for any WPF control.

FrameworkElement

The FrameworkElement class was introduced in an earlier article in this tutorial. FrameworkElement is a standard .NET framework base class that is inherited by all controls, including the layout controls that were described previously. The class provides functionality related to the logical tree, styling, data binding and lifetime events.

In this short article we'll see three object lifetime events. These events are raised by all controls when they are initialised, loaded and unloaded. They allow you to work with controls at key stages, which can be essential in some situations.

In the first part of the article I'll describe the three events. This will be followed by some simple source code to demonstrate their use.

Initialized

The first object lifetime event raised by any control is Initialized. The event is raised at around the time that the control's constructor is executed. If you capture this event, you can access all of the properties of the control that raised it. However, it is possible that properties of other controls will not yet be ready and properties of the current control that are set using data binding will not yet be evaluated. Child controls of the item that generate the event will be in a similar state; these are represented as properties of the parent control so are guaranteed to be initialized to the same degree. Parent controls may not be initialized at all.

The Initialized event can be useful when these limitations will not cause a problem, and where you wish to executed the event handlers as early as possible.

Loaded

Loaded is probably more commonly used than Initialized. It is raised later in the object's lifetime, before the control is rendered to the screen but after the entire logical tree has been initialised. Although the controls have not been displayed at the point of raising, all of the properties are calculated, including those relating to positioning and layout. All of the values can be accessed safely. Standard data binding will be complete also, allowing you to interrogate the bound data.

The Loaded event is raised by the logical tree's root element first. This is often a Window. Next, each of the child controls produces the event until the entire tree of elements has been processed.

Unloaded

Unloaded is the last event in an object's lifetime. It is raised when a control is removed from the logical tree. This may happen, with a corresponding Loaded event, when a user action causes a control to be reloaded. For example, if the desktop theme is changed via the Control Panel.

Unloaded is not guaranteed to be raised for a control. If a window is closing or the application is exiting, the event is not triggered; if you need to perform some clean-up on exit, you must use an alternative approach.

Example Code

To demonstrate the use of the three object lifetime events, create a new WPF application project named, "FrameworkElementLifetimeDemo". Replace the XAML in the automatically added window with the following. This does not create any visible controls but attaches the three events to a Grid.

<Window x:Class="FrameworkElementLifetimeDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FrameworkElement Demo"
        Width="250"
        Height="200">
    
    <Grid Initialized="Grid_Initialized"
          Loaded="Grid_Loaded"
          Unloaded="Grid_Unloaded">      
    </Grid>
</Window>

We can now add the code for the events. For simplicity, we'll make each event display a message in a standard message box. Switch to the code behind the WPF window and add the following three methods:

private void Grid_Initialized(object sender, EventArgs e)
{
    MessageBox.Show("Initialized");
}

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Loaded");
}

private void Grid_Unloaded(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Unloaded");
}

Run the program to see the results. You should first see a message indicating that the Grid was initialised, followed by another showing that it was loaded. If you close the program, you will not see the "Unloaded" message.

To see that the Unloaded event can be triggered, run the program again and change the desktop theme using the Control Panel. You should see that both the Unloaded and Loaded events are raised as the window is reset for the new theme.

2 September 2013