Partager via


DataTemplate.LoadContent Méthode

Définition

Crée les objets UIElement dans dataTemplate.

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

Retours

UIElement racine du DataTemplate.

Exemples

Les exemples suivants illustrent l’utilisation de la méthode LoadContent pour modifier l’apparence d’une bordure au moment de l’exécution. L’exemple crée un ListBox qui contient les nombres 1 à 10. Lorsque l’utilisateur sélectionne un élément dans la Zone de liste, la bordure affiche le nombre sélectionné. Si l’utilisateur sélectionne un nombre pair, celui-ci est rouge et comporte un cercle vert. Si l’utilisateur sélectionne un nombre impair, celui-ci est bleu et comporte un carré violet autour.

<StackPanel  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>

  <Border Name="selectedItemDisplay"
          Width="50" Height="50"/>

  <ListBox Name="numberList" SelectionChanged="ListBox_SelectionChanged">
    <ListBoxItem Content="1"/>
    <ListBoxItem Content="2"/>
    <ListBoxItem Content="3"/>
    <ListBoxItem Content="4"/>
    <ListBoxItem Content="5"/>
    <ListBoxItem Content="6"/>
    <ListBoxItem Content="7"/>
    <ListBoxItem Content="8"/>
    <ListBoxItem Content="9"/>
    <ListBoxItem Content="10"/>
  </ListBox>

</StackPanel>
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    SelectDataTemplate(lbi.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 ComboBox.
        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;
}
Private Sub ListBox_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
    Dim lbi As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    SelectDataTemplate(lbi.Content)

End Sub

Private Sub SelectDataTemplate(ByVal value As Object)
    Dim numberStr As String = CType(value, String)

    If Not numberStr Is Nothing Then
        Dim num As Integer

        Try
            num = Convert.ToInt32(numberStr)
        Catch
            Return
        End Try

        Dim template As DataTemplate


        ' Select one of the DataTemplate objects, based on the 
        ' value of the selected item in the ComboBox.
        If num Mod 2 <> 0 Then
            template = CType(rootStackPanel.Resources("oddNumberTemplate"), DataTemplate)
        Else
            template = CType(rootStackPanel.Resources("evenNumberTemplate"), DataTemplate)
        End If

        selectedItemDisplay.Child = CType(template.LoadContent(), UIElement)
        Dim tb As TextBlock = FindVisualChild(Of TextBlock)(selectedItemDisplay)
        tb.Text = numberStr

    End If
End Sub

Private Function FindVisualChild(Of childItem As DependencyObject) _
        (ByVal obj As DependencyObject) As childItem

    Dim i As Integer

    For i = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1

        Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)
        If ((Not child Is Nothing) And (TypeOf child Is childItem)) Then
            Return child
        End If

        Dim childOfChild As childItem = Me.FindVisualChild(Of childItem)(child)
        If (Not childOfChild Is Nothing) Then
            Return childOfChild
        End If

    Next i

    Return Nothing

End Function

Remarques

Lorsque vous appelez LoadContent, les objets UIElement dans dataTemplate sont créés et vous pouvez les ajouter à l’arborescence visuelle d’un autre UIElement.

S’applique à