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標籤不工作的任何原因?
感謝雷切爾。這是我錯過的那麼明顯的事情。現在我更好地理解了TabControl的怪異行爲。 – digitguy 2012-02-23 07:40:22