2012-02-22 123 views
2

我有一個TabControl,並在其中有一個具有ContentControl的TabItem。這個ContentControl應用了一個數據模板。該代碼是在這裏:DataTemplate中的RelativeSource與TabControl一起使用,但不與TabItem一起使用

<TabControl x:Name="tabControl1" Tag="Giving URI here works"> 
         <TabItem x:Name="tabItem1" Tag="Giving URI here doesnt work"> 
          <ContentControl ContentTemplate="{StaticResource myOptionsDataTemplate}"> 
           <StackPanel> 
            <TextBlock Text="Some Text" /> 
           </StackPanel> 
          </ContentControl> 
         </TabItem> 
</TabControl> 

而且數據模板是:

<DataTemplate x:Key="myOptionsDataTemplate"> 
     <Border> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 
       <DockPanel LastChildFill="True"> 
        <Image Source="{Binding Path=Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabItem}}}" 
          Width="32" Height="32" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          DockPanel.Dock="Left" 
          Margin="0,0,4,0"/> 
        <Label Content="Some Text" 
          /> 
       </DockPanel> 
       <ContentControl Grid.Row="2" Content="{TemplateBinding ContentControl.Content}"/> 
      </Grid> 
     </Border> 
    </DataTemplate> 

請注意,在DataTemplate中的圖像源是的TabItem的標籤。這不起作用。但是如果我改變Source來採用TabControl的Tag,它就可以工作。

爲什麼使用TabItem標籤不工作的任何原因?

回答

3

如果您使用類似Snoop的東西來查看繪製的實際可視樹,您會看到TabControl的標題和內容位於不同的區域,而TabItems只存在於標題區域,而不是內容區域。內容區域只包含當前SelectedItem。

enter image description here

+0

感謝雷切爾。這是我錯過的那麼明顯的事情。現在我更好地理解了TabControl的怪異行爲。 – digitguy 2012-02-23 07:40:22

相關問題