简体   繁体   English

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

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

I am using MahApps TabControl. 我正在使用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. 如果我从xaml添加项目,则可以设置“ CloseButtonEnabled = true”,并且显示“关闭”按钮,当我尝试绑定ItemSource时,不显示关闭按钮。 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 . 这里的问题是您覆盖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. 所述MetroTabItemBasedOn="{StaticResource MetroTabItem}"是一个样式,不类型。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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