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

时间:2016-11-30 08:40:00

标签: c# wpf mahapps.metro

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

以下是我的示例代码:

SELECT "VBUK-UVALS",
       CASE ("VBUK-UVALS") WHEN 'A' THEN 'Closed'
                           WHEN 'B' THEN 'Open' -- CASE ("VBUK-UVALS") removed
                           ELSE 'Other'
       END AS "ColumnA"
FROM   "Local"."INFOSET"."ZCA_TESTAR"

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

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

答案 1 :(得分:1)

此处的问题是您覆盖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>

MetroTabItem中的BasedOn="{StaticResource MetroTabItem}"是一种风格,而不是类型。

希望有所帮助!