DataTemplate 中的RelativeSource 可与TabControl 配合使用,但不能与TabItem 配合使用
我有一个 TabControl,其中有一个具有 ContentControl 的 TabItem。该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>
注意数据模板中的图像源是TabItem 的Tag。这不起作用。但是,如果我更改源以获取 TabControl 的标签,它就可以工作。
为什么使用 TabItem 的标签不起作用?
I am having a TabControl and within it a TabItem having a ContentControl. This ContentControl is applied a datatemplate. The code is here:
<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>
And the data template is:
<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>
Notice the Image Source in the datatemplate is Tag of TabItem. This isn't working. But if I change the Source to take the Tag of TabControl it works.
Any reason why using Tag of TabItem is not working??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用类似 Snoop 的内容来查看实际绘制的可视化树,您将看到 TabControl 的标题和 Content 位于不同的区域,并且 TabItem 只存在于 Header 区域,而不存在于 Content 区域。内容区域仅保存当前的 SelectedItem。
If you use something like Snoop to look at the actual Visual Tree getting drawn, you'll see that TabControl's Header and Content are in separate areas, and the TabItems only exist in the Header area, not the Content area. The Content area only holds the currently SelectedItem.