簡體   English   中英

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>

所述MetroTabItemBasedOn="{StaticResource MetroTabItem}"是一個樣式,不類型。

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM