ContentControl.ContentTemplateSelector ContentControl.ContentTemplateSelector ContentControl.ContentTemplateSelector ContentControl.ContentTemplateSelector Property

定义

获取或设置一个模板选择器,以使应用程序编写器能够提供自定义模板选择逻辑。Gets or sets a template selector that enables an application writer to provide custom template-selection logic.

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

属性值

数据模板选择器。A data template selector. 默认值为 nullThe default value is null.

示例

下面的示例演示如何使用 ContentTemplateSelector 属性。The following example shows how to use the ContentTemplateSelector property. 此示例将ComboBox中的选定项绑定Content到的属性Label, 后者继承自ContentControlThis example binds the selected item in a ComboBox to the Content property of a Label, which inherits from ContentControl. 当用户选择低于5的值时, 所选项的值将显示在中Label的黑色方块内。When the user selects a value below 5, the value of the selected item appears in a black square in the Label. 当用户选择的值为5或更高时, 该值将显示在绿色椭圆中。When the user selects a value that is 5 or above, the value appears in a green ellipse. 该示例DataTemplate通过创建两个对象和一个DataTemplateSelector来实现此目的, ContentTemplateSelector该对象设置为属性, 并DataTemplate根据所选项的值选择相应的。The example accomplishes this by creating two DataTemplate objects and a DataTemplateSelector, which is set to the ContentTemplateSelector property and chooses the appropriate DataTemplate based on the value of the selected item.

<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>
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;
        }
    }
}
Public Class NumberDataTemplateSelector
    Inherits DataTemplateSelector

    Public Property NumberTemplate As DataTemplate
    Public Property LargeNumberTemplate As DataTemplate

    Public Overrides Function SelectTemplate(ByVal item As Object, _
                    ByVal container As DependencyObject) As DataTemplate

        ' Nothing can be passed by IDE designer
        if (item Is Nothing) Then
            Return Nothing
        End If

        Dim num = Convert.ToInt32(CStr(item))

        ' Select one of the DataTemplate objects, based on the 
        ' value of the selected item in the ComboBox.
        If num < 5 Then
            Return NumberTemplate

        Else
            Return LargeNumberTemplate
        End If

    End Function 'SelectTemplate
End Class 'NumderDataTemplateSelector 

注解

通常, DataTemplateSelector当您对同一类型的对象具有多DataTemplate个时, 您需要提供自己的逻辑, 以便基于每个数据对象的DataTemplate属性来选择要应用的。Typically, you create a DataTemplateSelector when you have more than one DataTemplate for the same type of objects and you want to supply your own logic to choose a DataTemplate to apply based on the properties of each data object. 请注意, 如果您具有不同类型的对象, 则可以DataTypeDataTemplate上设置属性。Note that if you have objects of different types you can set the DataType property on the DataTemplate. 如果这样做, 则无需创建DataTemplateSelectorIf you do that, then there is no need to create a DataTemplateSelector. 此外, 如果您的对象具有相同类型但具有不同的属性, 则还可以考虑使用DataTrigger或数据转换器。Furthermore, if you have objects of the same type but with different properties, you can also consider using a DataTrigger or a data converter. 有关详细信息,请参阅 数据模板化概述For more information, see Data Templating Overview.

若要创建模板选择器, 请创建一个继承自DataTemplateSelector的类, SelectTemplate并重写方法。To create a template selector, create a class that inherits from DataTemplateSelector and override the SelectTemplate method. 定义类后, 可以将类的实例分配给元素的模板选择器属性。After your class is defined, you can assign an instance of the class to the template selector property of your element.

如果同时设置ContentTemplateSelectorContentTemplate和属性, 则忽略此属性。If both the ContentTemplateSelector and the ContentTemplate properties are set, then this property is ignored.

XAML 属性用法XAML Attribute Usage

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

XAML 值XAML Values

ResourceExtensionResourceExtension
标识如何引用模板资源 ( StaticResourceDynamicResource) 的标记扩展。A markup extension that identifies how to reference the template resource, either StaticResource or DynamicResource. 请参阅XAML 资源See XAML Resources.

TemplateSelectorKeyTemplateSelectorKey
标识所请求模板选择器的键。The key that identifies the requested template selector. 键引用中ResourceDictionary的现有资源。The key refers to an existing resource in a ResourceDictionary.

依赖项属性信息Dependency Property Information

标识符字段Identifier field ContentTemplateSelectorProperty
元数据属性设置为trueMetadata properties set to true NoneNone

适用于