I want to do something like these buttons instead of the standard TabItem in TabControl .
I override ContentControl for TabItem , but I can’t figure out how to make the rectangle's width be slightly larger than the text and the tabs are next to each other? With this style, TabItem just stretch across the entire window.
xaml:
<Style TargetType="{x:Type TabControl}"> <Setter Property="ItemContainerStyle"> <Setter.Value> <Style TargetType="{x:Type TabItem}"> <Setter Property="Background" Value="#0099ff"/> <Setter Property="Foreground" Value="#0080d6"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="2*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Rectangle Grid.Column="0" Grid.ColumnSpan="2" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight, Converter={StaticResource HeightOffsetConverter}}" Fill="{TemplateBinding Foreground}" RadiusX="3" RadiusY="3" VerticalAlignment="Bottom"/> <Rectangle Grid.Column="0" Grid.ColumnSpan="2" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight, Converter={StaticResource HeightOffsetConverter}}" Fill="{TemplateBinding Background}" RadiusX="3" RadiusY="3" VerticalAlignment="Top"/> <TextBlock Grid.Column="0" Text="{TemplateBinding Header}" Foreground="White"/> <Rectangle Grid.Column="0" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight, Converter={StaticResource HeightOffsetConverter}}" Fill="White" RadiusX="3" RadiusY="3" Opacity="0.1" VerticalAlignment="Top"/> <Rectangle Grid.Column="1" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight, Converter={StaticResource HeightOffsetConverter}}" Fill="Black" RadiusX="3" RadiusY="3" Opacity="0.1" VerticalAlignment="Top"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Setter.Value> </Setter> </Style> converter:
public class HeightOffsetConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (double)value - 2; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

