DataTemplate中的RelativeSource与TabControl配合使用,但不适用于TabItem [英] RelativeSource in DataTemplate works with TabControl but not with TabItem

查看:172
本文介绍了DataTemplate中的RelativeSource与TabControl配合使用,但不适用于TabItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TabControl,并在其中有一个具有ContentControl的TabItem。此ContentControl应用于数据表。代码在这里:

 < TabControl x:Name =tabControl1Tag =给这里的URI工作> 
< TabItem x:Name =tabItem1Tag =给这里的URI不起作用>
< 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 =32Height =32
Horizo​​ntalAlignment =Left
VerticalAlignment =Top
DockPanel.Dock =Left
Margin =0,0,4,0/>
< Label Content =Some Text
/>
< / DockPanel>
< ContentControl Grid.Row =2Content ={TemplateBinding ContentControl.Content}/>
< / Grid>
< / Border>
< / DataTemplate>

请注意,数据表中的图像源是TabItem的标签。这不行。但是如果我更改Source来获取TabControl的标签,它可以正常工作。



使用TabItem标签的原因不起作用?

解决方案

p>如果您使用类似 Snoop 的内容来查看实际的Visual Tree绘制,您会看到TabControl的头文件和内容在不同的区域,TabItems仅存在于标题区域,而不存在于内容区域中。内容区域只保存当前SelectedItem。




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??

解决方案

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.

这篇关于DataTemplate中的RelativeSource与TabControl配合使用,但不适用于TabItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆