Answer the question
In order to leave comments, you need to log in
Why can't I set the ItemTemplate?
I write:
<DataTemplate x:Key="ClosableTabItemHeaderTemplate">
<DataTemplate.Resources>
<BooleanToVisibilityConverter x:Key="B2V"/>
</DataTemplate.Resources>
<Grid Background="Transparent" HorizontalAlignment="Stretch" MinWidth="100" ToolTip="{Binding}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ContentPresenter Content="{Binding}" Grid.Column="0" HorizontalAlignment="Center"/>
<Button Name="CLOSE"
Command="{Binding Path=CloseCommand}"
Width="15"
Height="15"
Grid.Column="1"
Template="{StaticResource CloseButtonTemplate}"
Visibility="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=TabItem},
Converter={StaticResource B2V}}"/>
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="CLOSE" Property="Visibility" Value="Visible"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
<TabControl ItemTemplate="{StaticResource ClosableTabItemHeaderTemplate}">
<TabControl.Items>
<TabItem Header="This is test tab item with longer name." Width="100"/>
<TabItem Header="Short name" Width="100"/>
</TabControl.Items>
</TabControl>
Answer the question
In order to leave comments, you need to log in
It seems to me that the problem is that you don't quite understand the difference between TabControl.ItemTemplate and TabItem.HeaderTemplate .
Judging by the template, you are trying to add a button to close the tab in the header header.
If so, then the following code should be used for this purpose:
<Grid>
<Grid.Resources>
<DataTemplate x:Key="ClosableTabItemHeaderTemplate">
<DataTemplate.Resources>
<BooleanToVisibilityConverter x:Key="B2V"/>
</DataTemplate.Resources>
<Grid Background="Transparent" HorizontalAlignment="Stretch" MinWidth="100" ToolTip="{Binding}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ContentPresenter Content="{Binding}" Grid.Column="0" HorizontalAlignment="Center"/>
<Button Name="CLOSE"
Command="{Binding Path=CloseCommand}"
Width="15"
Height="15"
Grid.Column="1"
Template="{StaticResource CloseButtonTemplate}"
Visibility="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=TabItem},
Converter={StaticResource B2V}}"/>
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="CLOSE" Property="Visibility" Value="Visible"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
<Style TargetType="TabItem">
<Setter Property="HeaderTemplate" Value="{StaticResource ClosableTabItemHeaderTemplate}" />
</Style>
</Grid.Resources>
<TabControl>
<TabControl.Items>
<TabItem Header="This is test tab item with longer name." Width="100"/>
<TabItem Header="Short name" Width="100"/>
</TabControl.Items>
</TabControl>
</Grid>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question