#915 – Delaying Generation of Expander Content

If you have a lot of content to display when an Expander is expanded and you want to delay loading of the content until the user actually opens the Expander, you can use the Expander’s Expanded event.

In the example below, the Expander contains a TextBlock, which we only load in the handler for the Expanded event.

        <Expander BorderBrush="DarkGray" Margin="0,5"
                  Header="Expand to See Something"
            <TextBlock Name="txtInfo"/>
        private void Expander_Expanded(object sender, RoutedEventArgs e)
            txtInfo.Text = string.Format("You expanded me at {0}", DateTime.Now);

When we run this example, the date/time displayed is the date/time that you expand the Expander.