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 Attribute Usage
<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 |
無 |