wpf TabControl.ItemTemplate:将 TabItem.Header.Text 设置为 StringFormat 的 MultiBinding
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1870564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TabControl.ItemTemplate: set TabItem.Header.Text to a MultiBinding with StringFormat
提问by Shimmy Weitzhandler
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.
如何将 TabItem.Header 设置为从几个字段中获取的绑定,每个绑定以不同的大小显示,全部位于原始标题文本的位置;不覆盖标题的默认样式和行为 - 我只需要文本。
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.
我试图设置它的模板,但随后它创建了一个包含内部控件的矩形,这个矩形对用户点击没有响应,并且还有控件样式,我希望这个控件不可见,只有它的文本应该可见.
I've tried the following:
我尝试了以下方法:
<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>
But it doesn't show anything.
但它没有显示任何东西。
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.
我还尝试将 HeaderTemplate 设置为 DataTemplate 但发生的情况是,DataTemplate 覆盖了 TabItem 样式,当我单击文本时,它不会转到单击的选项卡,此外,未选择的选项卡看起来很有趣,我明白了浮动文本的矩形,而我希望它是透明的。
So, to summarize up my question, I want to set TabItem.Header.Text to a MultiBinding with StringFormat.
所以,总结一下我的问题,我想用 StringFormat 将 TabItem.Header.Text 设置为 MultiBinding。
回答by rmoore
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.
TabControl 包含一个 ContentTemplate 属性以及它从 ItemsControl 继承的 ItemTemplate。它使用 ContentTemplate 来区分 Content 区域中显示的内容,而 ItemTemplate 则定义了 Header 的模板。此外,您的 ItemSource 中的每个 Item 都会自动包装在一个 TabItem 中;它不需要在 ItemTemplate 中重新创建,因为这会尝试在您注意到的时候将 TabItem 放在 Header 中。
Instead of re-creating a TabItem inside the ItemTemplate, use the ItemTemplate to define your Header content, and the ContentTemplate to define your Content.
不是在 ItemTemplate 中重新创建 TabItem,而是使用 ItemTemplate 来定义您的 Header 内容,并使用 ContentTemplate 来定义您的内容。
<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>
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.
在您的第一段中,您提到要在 Header 的绑定部分设置不同的大小。如果您确实想这样做,您将无法像上面那样使用单个 Binding 或 MultiBinding 来设置 Text。相反,您可以嵌套 TextBlocks 以使用不同的格式实现这一点。
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock Text="{Binding Title}"
FontSize="12" />
<Run Text="--" />
<TextBlock Text="{Binding Category.Title}"
FontSize="10" />
</TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>