niks niks - 7 days ago 5
C# Question

Mahapps TabControl, can't set CloseButtonEnabled = true when using ItemSource ={Binding..}

I am using MahApps TabControl. If I add Items from xaml I can set "CloseButtonEnabled =true" and the Close button is showed, when I try to bind ItemSource, the close button does not appear. Any ideas, how I could solve this problem?

Here is my sample code:

<Window.Resources>
<Style BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type Controls:MetroTabItem}">
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="15"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="CloseButtonEnabled" Value="True"/>
</Style>
</Window.Resources>

<Controls:MetroTabControl ItemsSource="{Binding AvailableFiles}" SelectedIndex="{Binding SelectedIndex}" Grid.Row="1" >
<Controls:MetroTabControl.ItemTemplate >
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</Controls:MetroTabControl.ItemTemplate>
</Controls:MetroTabControl>

Answer

The problem here is that you override the complete style for the MetroTabItem.

If you only want additional changes then do this

<Window.Resources>
    <Style BasedOn="{StaticResource {x:Type Controls:MetroTabItem}}" TargetType="{x:Type Controls:MetroTabItem}">
        <Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="15"/>
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="CloseButtonEnabled" Value="True"/>
    </Style>
</Window.Resources>

The MetroTabItem in BasedOn="{StaticResource MetroTabItem}" is a style and not the type.

Hope that helps!