DataTemplate.LoadContent Method

Definition

Creates the UIElement objects in the DataTemplate.

public:
 virtual DependencyObject ^ LoadContent() = LoadContent;
DependencyObject LoadContent();
public DependencyObject LoadContent();
function loadContent()
Public Function LoadContent () As DependencyObject

Returns

The root UIElement of the DataTemplate.

Examples

The following examples demonstrate using the LoadContent method to change the appearance of a Border at run time. The example creates a ListView that contains the numbers 1 through 10. When the user selects an item in the ListView, the Border displays the selected number. If the user selects an even number, the number is red and has a green circle around it. If the user selects an odd number, the number is blue and has a purple square around it.

<StackPanel x:Name="rootStackPanel">
    <StackPanel.Resources>
        <DataTemplate x:Key="oddNumberTemplate">
            <Grid>
                <Rectangle Stroke="Purple" StrokeThickness="4"/>
                <TextBlock HorizontalAlignment="Center" 
                           VerticalAlignment="Center" 
                           FontSize="24" Foreground="Blue" 
                           FontWeight="Bold"/>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="evenNumberTemplate">
            <Grid>
                <Ellipse Stroke="Green" StrokeThickness="4"/>
                <TextBlock HorizontalAlignment="Center" 
                           VerticalAlignment="Center" 
                           FontSize="24" Foreground="Red" 
                           FontWeight="Bold"  />
            </Grid>
        </DataTemplate>
    </StackPanel.Resources>

    <ListView x:Name="numberList" 
              SelectionChanged="ListView_SelectionChanged" 
              HorizontalAlignment="Center">
        <ListViewItem Content="1"/>
        <ListViewItem Content="2"/>
        <ListViewItem Content="3"/>
        <ListViewItem Content="4"/>
        <ListViewItem Content="5"/>
        <ListViewItem Content="6"/>
        <ListViewItem Content="7"/>
        <ListViewItem Content="8"/>
        <ListViewItem Content="9"/>
        <ListViewItem Content="10"/>
    </ListView>

    <Border x:Name="selectedItemDisplay" 
            Width="50" Height="50"/>
</StackPanel>
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListViewItem lvi = ((sender as ListView).SelectedItem as ListViewItem);
    SelectDataTemplate(lvi.Content);
}

private void SelectDataTemplate(object value)
{
    string numberStr = value as string;

    if (numberStr != null)
    {
        int num;

        try
        {
            num = Convert.ToInt32(numberStr);
        }
        catch
        {
            return;
        }

        DataTemplate template;

        // Select one of the DataTemplate objects, based on the 
        // value of the selected item in the ListView.
        if (num % 2 != 0)
        {
            template = rootStackPanel.Resources["oddNumberTemplate"] as DataTemplate;
        }
        else
        {
            template = rootStackPanel.Resources["evenNumberTemplate"] as DataTemplate;
        }

        selectedItemDisplay.Child = template.LoadContent() as UIElement;
        TextBlock tb = FindVisualChild<TextBlock>(selectedItemDisplay);
        tb.Text = numberStr;
    }
}

private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);

        if (child != null && child is childItem)
        {
            return (childItem)child;
        }
        else
        {
            childItem childOfChild = FindVisualChild<childItem>(child);
            if (childOfChild != null)
                return childOfChild;
        }
    }
    return null;
}

Remarks

When you call LoadContent, the UIElement objects in the DataTemplate are created, and you can add them to the visual tree of another UIElement.

Applies to