【发布时间】:2012-02-22 11:57:34
【问题描述】:
我有一个 TabControl,其中一个 TabItem 有一个 ContentControl。此 ContentControl 应用了数据模板。代码在这里:
<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中的Image Source是TabItem的Tag。这是行不通的。但是,如果我更改 Source 以获取 TabControl 的标签,它就可以工作。
为什么使用 TabItem 的标签不起作用??
【问题讨论】:
标签: wpf data-binding datatemplate tabcontrol tabitem