2013-04-29 60 views
1

我試圖在我的TabControl的模板中獲得selected itemheader,但我無法做到。我嘗試了幾種解決方案,但他們沒有工作:如何在TabControl模板中獲取所選項目的標題?

無結果:

<ContentPresenter ContentSource="{TemplateBinding SelectedItem}"/> 

錯誤編譯(因爲的SelectedItem類型爲對象,而不是HeaderedContentControl):

<ContentPresenter ContentSource="{TemplateBinding SelectedItem.Header}"/> 

很容易得到它在C#中,但我想把它放在我的TabControl模板中。

有人有什麼想法嗎?

感謝

回答

5

這應該做的伎倆:

<TextBlock Text="{Binding SelectedItem.Header, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" /> 

這裏是一個完整的XAML證明它:

<Window x:Class="WpfApplication21.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:WpfApplication21" 
      Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ControlTemplate x:Key="TabControlControlTemplate" TargetType="{x:Type TabControl}"> 
      <StackPanel> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="Selected Item header: " /> 
        <TextBlock Text="{Binding SelectedItem.Header, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" /> 
       </StackPanel> 

       <Grid ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition x:Name="ColumnDefinition0"/> 
         <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition x:Name="RowDefinition0" Height="Auto"/> 
         <RowDefinition x:Name="RowDefinition1" Height="*"/> 
        </Grid.RowDefinitions> 
        <TabPanel x:Name="HeaderPanel" Grid.Column="0" IsItemsHost="True" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/> 
        <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local"> 
         <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Border> 
       </Grid> 
      </StackPanel> 
      <ControlTemplate.Triggers> 
       <Trigger Property="TabStripPlacement" Value="Bottom"> 
        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/> 
        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/> 
        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> 
        <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/> 
        <Setter Property="Margin" TargetName="HeaderPanel" Value="2,0,2,2"/> 
       </Trigger> 
       <Trigger Property="TabStripPlacement" Value="Left"> 
        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/> 
        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/> 
        <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/> 
        <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/> 
        <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/> 
        <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/> 
        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> 
        <Setter Property="Height" TargetName="RowDefinition1" Value="0"/> 
        <Setter Property="Margin" TargetName="HeaderPanel" Value="2,2,0,2"/> 
       </Trigger> 
       <Trigger Property="TabStripPlacement" Value="Right"> 
        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/> 
        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/> 
        <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/> 
        <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/> 
        <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/> 
        <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/> 
        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> 
        <Setter Property="Height" TargetName="RowDefinition1" Value="0"/> 
        <Setter Property="Margin" TargetName="HeaderPanel" Value="0,2,2,2"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 

    </Window.Resources> 

    <TabControl Template="{DynamicResource TabControlControlTemplate}"> 
     <TabItem Header="Tab 1" /> 
     <TabItem Header="Tab 2" /> 
     <TabItem Header="Tab 3" /> 
     <TabItem Header="Tab 4" /> 
    </TabControl> 
</Window> 

Header binding

+0

謝謝,它的工作原理!但是,如果您將控件放在TabItem的標題中(如圖像,面板),則TextBlock無法幫助您。我嘗試了ContentPresenter和ContentControl,但它沒有工作......任何想法? – Max 2013-04-29 16:52:33

+0

不客氣。你可以使用一個ContentControl中: Sisyphe 2013-04-29 17:07:16

+0

當我嘗試把WrapPanel在我的頭,如果我使用ContentControl中在我的模板,我可以看到後它「選定條目標題:」但它會從的TabPanel在前看不見。這很奇怪... – Max 2013-04-29 20:17:40

0

我試圖完成類似的東西能夠在開始的時候用上面的解決方案來做到這一點g圍繞註釋中提到的問題,通過使用Tag而不是Header屬性(它包含一個控件)。

我把我所需要的的TabItem的標記文本,並顯示在我的模板。希望這有助於某人。

<TextBlock Text="{Binding SelectedItem.Tag, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}"> 

...

<TabItem Tag="Header"> 
    <TabItem.Header> 
     <Grid>... 
相關問題