Share via


DataTemplate.LoadContent メソッド

定義

DataTemplateUIElement オブジェクトを作成します。

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

戻り値

DataTemplate のルート UIElement

次の例では、LoadContent メソッドを使用して実行時に Border の外観を変更する方法を示します。 この例では、1 ~ 10 の数値を含む ListView を作成します。 ユーザーが 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 のビジュアル ツリーに追加できます。

適用対象