Mahapps TabControl,在使用 ItemSource ={Binding..} 时无法设置 CloseButtonEnabled = true

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

我正在使用 MahApps TabControl。如果我从 xaml 添加项目,我可以设置 "CloseButtonEnabled =true" 并显示关闭按钮,当我尝试绑定 ItemSource 时,关闭按钮不会出现。有什么想法,我该如何解决这个问题?

这是我的示例代码:

<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>

您可以尝试以下方法:

<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>

这里的问题是您覆盖了 MetroTabItem 的完整样式。

如果您只想进行其他更改,请执行此操作

<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>

BasedOn="{StaticResource MetroTabItem}"中的MetroTabItem是样式,不是类型。

希望对您有所帮助!