简体   繁体   中英

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>

You can try following approach:

<Window.Resources>
<Style x:Key="MyCustomTabItem" 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}" ItemContainerStyle="{StaticResource MyCustomTabItem}" SelectedIndex="{Binding SelectedIndex}"  Grid.Row="1" >           
    <Controls:MetroTabControl.ItemTemplate >
        <DataTemplate>
            <TextBlock Text="{Binding Title}" />
        </DataTemplate>
    </Controls:MetroTabControl.ItemTemplate>
</Controls:MetroTabControl>

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM