DataTemplate.LoadContent 方法

定义

DataTemplate 中创建 UIElement 对象。

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

返回

DataTemplate 的根 UIElement

示例

以下示例演示如何使用 LoadContent 方法在运行时更改 Border 的外观。 该示例创建一个 ListView ,其中包含数字 1 到 10。 当用户在 ListView 中选择项目时, 边框 将显示所选数字。 如果用户选择偶数,则数字为红色,周围有一个绿色圆圈。 如果用户选择奇数,则数字为蓝色,周围有一个紫色的正方形。

<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;
}

注解

调用 LoadContent 时,将创建 DataTemplate 中的 UIElement 对象,并且可以将它们添加到另一个 UIElement 的可视化树中。

适用于