TabControl.ItemTemplate:将 TabItem.Header.Text 设置为具有 StringFormat 的 MultiBinding [英] TabControl.ItemTemplate: set TabItem.Header.Text to a MultiBinding with StringFormat

查看:15
本文介绍了TabControl.ItemTemplate:将 TabItem.Header.Text 设置为具有 StringFormat 的 MultiBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 TabItem.Header 设置为从几个字段中获取的绑定,每个绑定以不同的大小显示,全部位于原始标题文本的位置;不覆盖标题的默认样式和行为 - 我只需要文本.

How do I set the TabItem.Header to bindings taken from few fields, each binding shown in a different size, all in the place of the original header text; without overriding the default style and behavior of the header - I only need the text.

我尝试设置它的模板,但随后它创建了一个包含内部控件的矩形,并且这个矩形对用户点击没有响应,并且还具有控件样式,我希望这个控件是不可见的,只有它的文本应该是可见的.

I tried to set its template but then it creates a rectangle that contains the inner controls, and this rectangle is not responsive for user clicks, and also have the control-style, I want this controls to be invisible, only its text should be visible.

我尝试了以下方法:

<TabControl ItemsSource="{Binding}">
    <TabControl.ItemTemplate>
         <DataTemplate>
             <TabItem>
                 <TabItem.Header>
                     <MultiBinding StringFormat="{}{0}-{1}">
                         <Binding Path="Title"/>
                         <Binding Path="Category.Title"/>
                     </MultiBinding>
                 </TabItem.Header>
                 <TabItem.Content>
                     <TextBlock>
                         Here is what is gonna be in the TabItem - not header
                     </TextBlock>
                 </TabItem.Content>
             </TabItem>
         </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

但它什么也没显示.

我也尝试将 HeaderTemplate 设置为 DataTemplate 但发生的情况是,DataTemplate 会覆盖 TabItem 样式,当我单击文本时它不会转到单击的选项卡,此外,未选择的选项卡看起来很有趣,我看到文本的矩形浮动,而我希望它是透明的.

I've also tried to set the HeaderTemplate to a DataTemplate but what happens is, the DataTemplate overrides the TabItem style and when I click the text it doesn't go to the clicked tab, besides, the unselected tabs look very funny, I see the rectangle of the text floating, while I want it to be transparent.

所以,总结一下我的问题,我想用 StringFormat 将 TabItem.Header.Text 设置为 MultiBinding.

So, to summarize up my question, I want to set TabItem.Header.Text to a MultiBinding with StringFormat.

推荐答案

TabControl 包含一个 ContentTemplate 属性以及它从 ItemsControl 继承的 ItemTemplate.它使用 ContentTemplate 来区分内容区域中显示的内容,而 ItemTemplate 定义了 Header 的模板.此外,您的 ItemSource 中的每个 Item 都将自动包装在 TabItem 中;它不需要在 ItemTemplate 中重新创建,因为这会尝试在您注意到的 Header 中放置一个 TabItem.

The TabControl contains a ContentTemplate property as well as the ItemTemplate that it inherits from ItemsControl. It uses the ContentTemplate to differentiate what is showing in the Content area while the ItemTemplate which defines the template for the Header. Additionally, each Item from your ItemSource will automatically be wrapped in a TabItem; it doesn't need to be re-created in the ItemTemplate, as that will attempt to place a TabItem inside the Header as you are noticing.

不要在 ItemTemplate 中重新创建 TabItem,而是使用 ItemTemplate 来定义您的 Header 内容,并使用 ContentTemplate 来定义您的 Content.

Instead of re-creating a TabItem inside the ItemTemplate, use the ItemTemplate to define your Header content, and the ContentTemplate to define your Content.

<TabControl ItemsSource="{Binding}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0}--{1}">
                        <Binding Path="Title" />
                        <Binding Path="Category.Title" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding MyContent}" />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

在您的第一段中,您提到要在页眉的绑定部分设置不同的大小.如果您确实想这样做,您将无法像上面那样使用单个 Binding 或 MultiBinding 来设置 Text.相反,您可以嵌套 TextBlocks 以使用不同的格式实现此目的.

In your first paragraph you mentioned wanting to set different sizes on the bound portions of the Header. If you do want to do that, you won't be able to use a single Binding or MultiBinding to set the Text as is done above. Instead you can nest TextBlocks to achieve this with different formatting for each.

<TabControl.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <TextBlock Text="{Binding Title}"
                       FontSize="12" />
            <Run Text="--" />
            <TextBlock Text="{Binding Category.Title}"
                       FontSize="10" />
        </TextBlock>
    </DataTemplate>
</TabControl.ItemTemplate>

这篇关于TabControl.ItemTemplate:将 TabItem.Header.Text 设置为具有 StringFormat 的 MultiBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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