ContentControl.ContentTemplateSelector プロパティ

定義

アプリケーション作成者がテンプレートを選択するためのカスタム ロジックを提供できるようにするテンプレート セレクターを取得または設定します。

C#
[System.ComponentModel.Bindable(true)]
public System.Windows.Controls.DataTemplateSelector ContentTemplateSelector { get; set; }

プロパティ値

データ テンプレート セレクター。 既定値は null です。

属性

ContentTemplateSelector プロパティを使用する方法を次の例に示します。 次の使用例は、 でComboBox選択した項目を からContent継承ContentControlする の Labelプロパティにバインドします。 ユーザーが 5 未満の値を選択すると、選択した項目の値が の黒い四角形に Label表示されます。 ユーザーが 5 以上の値を選択すると、値は緑色の省略記号で表示されます。 この例では、2 つの DataTemplate オブジェクトと DataTemplateSelectorを作成することでこれを実現します。これは、 プロパティに ContentTemplateSelector 設定され、選択した項目の値に基づいて適切な DataTemplate を選択します。

XAML
<Window.Resources>

  <!--Create two DataTemplate objects to be 
  selected by the DataTemplateSelector.-->
  <DataTemplate x:Key="numberTemplate">
    <Grid>
      <Rectangle Stroke="Black" />
      <TextBlock Margin="5" Text="{Binding}" FontSize="18"/>
    </Grid>
  </DataTemplate>

  <DataTemplate x:Key="largeNumberTemplate">
    <Grid>
      <Ellipse Stroke="Green" StrokeThickness="4"/>
      <TextBlock Margin="10" Text="{Binding}" FontSize="24" 
                 Foreground="Red" FontWeight="Bold" />
    </Grid>
  </DataTemplate>

  <local:NumberDataTemplateSelector x:Key="numberTemplateSelector"
                                    NumberTemplate="{StaticResource numberTemplate}"
                                    LargeNumberTemplate="{StaticResource largeNumberTemplate}"/>

</Window.Resources>
<StackPanel>

  <!--Bind the content of the Label to the selected item 
  in the ComboBox.-->
  <Label  Foreground="Black"
          Content="{Binding ElementName=numberList, Path=SelectedItem.Content}"
          ContentTemplateSelector="{StaticResource numberTemplateSelector}">
  </Label>

  <ComboBox Name="numberList">
    <ComboBoxItem>1</ComboBoxItem>
    <ComboBoxItem>2</ComboBoxItem>
    <ComboBoxItem>3</ComboBoxItem>
    <ComboBoxItem>4</ComboBoxItem>
    <ComboBoxItem>5</ComboBoxItem>
    <ComboBoxItem>6</ComboBoxItem>
    <ComboBoxItem IsSelected="True">7</ComboBoxItem>
    <ComboBoxItem>8</ComboBoxItem>
    <ComboBoxItem>9</ComboBoxItem>
    <ComboBoxItem>10</ComboBoxItem>
  </ComboBox>

</StackPanel>
C#
public class NumberDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate NumberTemplate { get; set; }
    public DataTemplate LargeNumberTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        // Null value can be passed by IDE designer
        if (item == null) return null;

        var num = Convert.ToInt32((string)item);
        
        // Select one of the DataTemplate objects, based on the 
        // value of the selected item in the ComboBox.
        if (num < 5)
        {
            return NumberTemplate;
        }
        else
        {
            return LargeNumberTemplate;
        }
    }
}

注釈

通常は、同じ種類のオブジェクトに複数DataTemplateの オブジェクトがあり、各データ オブジェクトのプロパティに基づいて 適用する をDataTemplate選択する独自のロジックを指定する場合に を作成DataTemplateSelectorします。 異なる型のオブジェクトがある場合は、 で プロパティを DataType 設定できることに DataTemplate注意してください。 これを行う場合は、 を作成 DataTemplateSelectorする必要はありません。 さらに、同じ型のオブジェクトがあり、プロパティが異なる場合は、 または データ コンバーターの DataTrigger 使用も検討できます。 詳細については、「 データ テンプレートの概要」を参照してください。

テンプレート セレクターを作成するには、 を継承 DataTemplateSelector するクラスを作成し、 メソッドをオーバーライドします SelectTemplate 。 クラスが定義されたら、クラスのインスタンスを要素のテンプレート セレクター プロパティに割り当てることができます。

プロパティと プロパティの両方 ContentTemplateSelectorContentTemplate 設定されている場合、このプロパティは無視されます。

XAML 属性の使用方法

<object ContentTemplateSelector="{ResourceExtension TemplateSelectorKey}"/>  

XAML 値

ResourceExtension
または のいずれかのテンプレート リソースStaticResourceDynamicResourceを参照する方法を識別するマークアップ拡張機能。 「XAML リソース」を参照してください。

TemplateSelectorKey
要求されたテンプレート セレクターを識別するキー。 キーは、 内の既存のリソースを ResourceDictionary参照します。

依存プロパティ情報

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

適用対象

製品 バージョン
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9