且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与需要帮助TabControl.ItemTemplate

更新时间:2022-06-10 07:33:54

该TabControl的包含的ContentTemplate属性以及ItemTemplate中,它自ItemsControl继承。它使用的ContentTemplate区分什么是同时的ItemTemplate它定义了页眉模板内容区域显示。此外,从你的ItemSource每个项目将自动被包裹在一个TabItem的;它并不需要在ItemTemplate重新创建,因为这将试图将一个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来定​​义页眉内容和的ContentTemplate来定​​义您的内容。

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>

在你的第一段你提到希望在页眉的边界部分设置不同的大小。如果你想这样做,你将无法使用单一的约束力或MultiBinding设置文本作为上面完成。相反,你可以窝的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>