ItemsControl.ItemTemplate プロパティ

定義

各項目を表示するために使用される DataTemplate を取得または設定します。

public:
 property System::Windows::DataTemplate ^ ItemTemplate { System::Windows::DataTemplate ^ get(); void set(System::Windows::DataTemplate ^ value); };
[System.ComponentModel.Bindable(true)]
public System.Windows.DataTemplate ItemTemplate { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.ItemTemplate : System.Windows.DataTemplate with get, set
Public Property ItemTemplate As DataTemplate

プロパティ値

DataTemplate

データ オブジェクトの視覚化を指定する DataTemplate。 既定値は、null です。

属性

次の例は、インラインを作成する方法を DataTemplate 示しています。 各DataTemplateデータ項目が内StackPanelの 3 つのTextBlock要素として表示されることを指定します。 この例では、データ オブジェクトは呼び出された Taskクラスです。 このテンプレートの各 TextBlock 要素は、クラスの Task プロパティにバインドされることに注意してください。

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=TaskName}" />
         <TextBlock Text="{Binding Path=Description}"/>
         <TextBlock Text="{Binding Path=Priority}"/>
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

次の例のように、再利用可能なオブジェクトになるように resources セクションで定義 DataTemplate する方が一般的です。

<Window.Resources>
<DataTemplate x:Key="myTaskTemplate">
  <StackPanel>
    <TextBlock Text="{Binding Path=TaskName}" />
    <TextBlock Text="{Binding Path=Description}"/>
    <TextBlock Text="{Binding Path=Priority}"/>
  </StackPanel>
</DataTemplate>
</Window.Resources>

これで、次の例のように、myTaskTemplate をリソースとして使用できるようになります。

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}"
         ItemTemplate="{StaticResource myTaskTemplate}"/>

完全なサンプルについては、「Introduction to Data Templating Sample」(データ テンプレート サンプルの概要) をご覧ください。

注釈

これを使用 ItemTemplate して、データ オブジェクトの視覚化を指定します。 ItemsControlコレクション オブジェクトにバインドされていて、それを使用して特定の表示命令をDataTemplate指定しない場合、各項目の結果の UI は、基になるコレクション内の各オブジェクトの文字列表現になります。

ItemsControl設定ItemTemplateすると、UI は次のように生成されます (例を使用)。ListBox

  1. コンテンツ生成中に ItemsPanel 、データ項目ごとにコンテナーを ItemContainerGenerator 作成するための要求が開始されます。 の場合 ListBox、コンテナーは ListBoxItem. ジェネレーターが呼び出してコンテナーを ItemsControl 準備します。

  2. 準備の一部は、のコピー ItemTemplate ListBox ContentTemplate ListBoxItemを含みます.

  3. すべてのContentControl型と同様に、a ListBoxItem ContentPresenterには ControlTemplate . テンプレートが適用されると、ContentPresenter ContentTemplate ContentTemplate ListBoxItem.

  4. 最後に、 ContentPresenter それ ContentTemplate 自体に適用され、UI が作成されます。

複数 DataTemplate の定義があり、プログラムで選択して適用 DataTemplateするロジックを指定する場合は、プロパティを ItemTemplateSelector 使用します。

ビジュアル ItemsControl のカスタマイズに優れた柔軟性を提供し、多くのスタイル設定とテンプレートのプロパティを提供します。 プロパティまたはプロパティをItemContainerStyleItemContainerStyleSelector使用して、データ項目を含む要素の外観に影響を与えるスタイルを設定します。 たとえば、 ListBox生成されたコンテナーはコントロールです ListBoxItem 。たとえば ComboBox、コントロールです ComboBoxItem 。 アイテムのレイアウトに影響を与える場合は、プロパティを使用します ItemsPanel 。 コントロールでグループ化を使用している場合は、or GroupStyleSelector プロパティをGroupStyle使用できます。

詳細については、「 データ テンプレートの概要」を参照してください。

XAML 属性の使用方法

<object ItemTemplate=" ResourceExtension TemplateResourceKey"/>  

XAML 値

ResourceExtension
StaticResource マークアップ拡張機能または DynamicResource マークアップ拡張機能のいずれか。 スタイル自体にシステム リソースやユーザー設定などの潜在的な実行時参照への参照が含まれている場合を除き、通常、パフォーマンスのためにスタイルへの StaticResource Markup Extension 参照をお勧めします。

TemplateResourceKey
リソースとして要求されているテンプレートを参照する x:Key ディレクティブ文字列値。

依存プロパティ情報

識別子フィールド ItemTemplateProperty
に設定されたメタデータ プロパティ true なし

適用対象

こちらもご覧ください