TabControl.ContentTemplateSelector 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置为选择用于显示控件内容的模板提供自定义逻辑的 DataTemplateSelector。
public:
property System::Windows::Controls::DataTemplateSelector ^ ContentTemplateSelector { System::Windows::Controls::DataTemplateSelector ^ get(); void set(System::Windows::Controls::DataTemplateSelector ^ value); };
public System.Windows.Controls.DataTemplateSelector ContentTemplateSelector { get; set; }
member this.ContentTemplateSelector : System.Windows.Controls.DataTemplateSelector with get, set
Public Property ContentTemplateSelector As DataTemplateSelector
属性值
ContentTemplateSelector。 默认值为 null
。
示例
下面的示例使用 ContentTemplateSelector 属性根据所选人员的家乡以不同的方式显示内容 TabItem 。
<DataTemplate x:Key="DetailTemplate">
<Border Width="300" Height="100" Margin="20"
BorderBrush="Aqua" BorderThickness="1" Padding="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="First Name:"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Last Name:"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Home Town:"/>
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=HomeTown}"/>
</Grid>
</Border>
</DataTemplate>
<DataTemplate x:Key="SeattleTemplate">
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="16"/>
</Style>
</DataTemplate.Resources>
<Border Width="300" Height="100" Margin="20"
BorderBrush="DarkSeaGreen" BorderThickness="3" Padding="15">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Please welcome"/>
<DockPanel Grid.Row="0" Grid.Column="1" >
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding Path=LastName}"/>
</DockPanel>
<TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="from the Emerald City!"/>
</Grid>
</Border>
</DataTemplate>
<!--Bind to the collection MyFriends and use a DataTemplateSelector
to specify the appearance of the content.-->
<TabControl Name="tabCtrl1" Width="400" Height="200"
ItemsSource="{Binding Source={StaticResource MyFriends}}"
ContentTemplateSelector="{StaticResource PersonSelector}"/>
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace TabControlContentTemplateSelector
{
// Create a class that represents a person.
// This class contains a person's first name, last name,
// and hometown.
public class Person : INotifyPropertyChanged
{
private string firstname;
private string lastname;
private string hometown;
public event PropertyChangedEventHandler PropertyChanged;
public Person()
{
}
public Person(string first, string last, string town)
{
this.firstname = first;
this.lastname = last;
this.hometown = town;
}
public override string ToString()
{
return firstname.ToString();
}
public string FirstName
{
get { return firstname; }
set
{
firstname = value;
OnPropertyChanged("FirstName");
}
}
public string LastName
{
get { return lastname; }
set
{
lastname = value;
OnPropertyChanged("LastName");
}
}
public string HomeTown
{
get { return hometown; }
set
{
hometown = value;
OnPropertyChanged("HomeTown");
}
}
protected void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
public class PersonTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
// The content of each TabItem is a Person object.
if (item is Person)
{
Person person = item as Person;
Window win = Application.Current.MainWindow;
// Select one of the DataTemplate objects, based on the
// person's home town.
if (person.HomeTown == "Seattle")
{
return win.FindResource("SeattleTemplate") as DataTemplate;
}
else
{
return win.FindResource("DetailTemplate") as DataTemplate;
}
}
return null;
}
}
Imports System.ComponentModel
Public Class Person
Implements INotifyPropertyChanged
' Events
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
' Methods
Public Sub New()
End Sub
Public Sub New(ByVal first As String, ByVal last As String, ByVal town As String)
Me._firstname = first
Me._lastname = last
Me._hometown = town
End Sub
Private Sub OnPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Public Overrides Function ToString() As String
Return Me._firstname.ToString
End Function
' Properties
Public Property FirstName() As String
Get
Return Me._firstname
End Get
Set(ByVal value As String)
Me._firstname = value
Me.OnPropertyChanged("FirstName")
End Set
End Property
Public Property HomeTown() As String
Get
Return Me._hometown
End Get
Set(ByVal value As String)
Me._hometown = value
Me.OnPropertyChanged("HomeTown")
End Set
End Property
Public Property LastName() As String
Get
Return Me._lastname
End Get
Set(ByVal value As String)
Me._lastname = value
Me.OnPropertyChanged("LastName")
End Set
End Property
' Fields
Private _firstname As String
Private _hometown As String
Private _lastname As String
End Class
Public Class PersonTemplateSelector
Inherits DataTemplateSelector
Public Overrides Function SelectTemplate(ByVal item As Object, _
ByVal container As DependencyObject) As DataTemplate
' The content of each TabItem is a Person object.
If TypeOf item Is Person Then
Dim person As Person = item
Dim win As Window = Application.Current.MainWindow
' Select one of the DataTemplate objects, based on the
' person's home town.
If person.HomeTown = "Seattle" Then
Return win.FindResource("SeattleTemplate")
Else
Return win.FindResource("DetailTemplate")
End If
End If
Return Nothing
End Function
End Class
注解
通常,为同一类型的对象创建 DataTemplateSelector 多个 DataTemplate 对象时,需要提供自己的逻辑,以便根据每个数据对象的属性选择 DataTemplate 应用。 请注意,如果有不同类型的对象,可以在该DataTemplate属性上设置DataType属性。 如果这样做,则无需创建 。DataTemplateSelector 此外,如果对象的类型相同,但具有不同的属性,还可以考虑使用 DataTrigger 或数据转换器。 有关详细信息,请参阅 数据模板化概述。
若要创建模板选择器,请创建继承自 DataTemplateSelector 该方法并重写方法的 SelectTemplate 类。 定义类后,可以将类的实例分配给元素的模板选择器属性。
ContentTemplateSelector如果同时设置了属性和ContentTemplate属性,则忽略此属性。
XAML 属性用法
<object ContentTemplateSelector="{ResourceExtension DataTemplateSelectorClassKey}"/>
XAML 属性元素用法
<object>
<object.ContentTemplateSelector>
<MyDataTemplateSelectorImplementation .../>
</object.ContentTemplateSelector>
</object>
XAML 值
ResourceExtension
下列项之一: StaticResource
或 DynamicResource
。 请参阅 XAML 资源。
DataTemplateSelectorClassKey
用于标识所请求的选择器实现的密钥。 该键是指实现实际 SelectTemplate 替代的派生类。 有关如何映射自定义类的信息,请参阅 WPF XAML 的 XAML 命名空间和命名空间映射。 还可以以编程方式将类的 DataTemplateSelector 实例作为资源添加到资源字典。
MyDataTemplateSelectorImplementation
派生 DataTemplateSelector 自和重写的 SelectTemplate类。 有关如何映射自定义类的信息,请参阅 WPF XAML 的 XAML 命名空间和命名空间映射。
依赖项属性信息
标识符字段 | ContentTemplateSelectorProperty |
元数据属性设置为 true |
无 |