Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override MetroTabItem controltemplate with close button #3129

Closed
Dimigergo opened this issue Dec 19, 2017 · 0 comments · Fixed by #3152
Closed

Override MetroTabItem controltemplate with close button #3129

Dimigergo opened this issue Dec 19, 2017 · 0 comments · Fixed by #3152
Assignees
Labels
Milestone

Comments

@Dimigergo
Copy link

Hello!

When I override the MetroTabItem controltemplate the close button doesn't works.

I have the following code:

<ControlTemplate x:Key="MainTabItem" TargetType="{x:Type mahapps:MetroTabItem}">
    <Border x:Name="Border"
            Margin="{TemplateBinding Padding}"
            BorderThickness="{TemplateBinding BorderThickness}"
            BorderBrush="{TemplateBinding BorderBrush}"
            Background="{TemplateBinding Background}"
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
        <StackPanel Orientation="Horizontal">
            <Ellipse VerticalAlignment="Top" Width="8" Height="8" 
                        Margin="0,5,2,0"
                        Visibility="{TemplateBinding helpers:CControlsHelper.LogTabItemType, Converter={StaticResource logTypeVisibilityConverter}}"
                        Fill="{TemplateBinding helpers:CControlsHelper.LogTabItemType, Converter={StaticResource logTypeFillConverter}}" />
            <ContentPresenter x:Name="ContentSite"
                                Margin="2,1,2,1"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                TextElement.FontSize="{TemplateBinding mahapps:ControlsHelper.HeaderFontSize}"
                                TextElement.FontStretch="{TemplateBinding mahapps:ControlsHelper.HeaderFontStretch}"
                                TextElement.FontWeight="{TemplateBinding mahapps:ControlsHelper.HeaderFontWeight}"
                                TextElement.Foreground="{TemplateBinding Foreground}"
                                ContentSource="Header"
                                RecognizesAccessKey="True" />
            <TextBlock Text="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource keyConverter}}" 
                        Foreground="{TemplateBinding helpers:CControlsHelper.TagColor}"
                        VerticalAlignment="Top" 
                        HorizontalAlignment="Right"
                        Margin="2,0,5,0"/>
            <Grid HorizontalAlignment="Right">
                <Button x:Name="PART_CloseButton"
                        Style="{DynamicResource ChromelessButtonStyle}"
                        Background="{DynamicResource GrayNormalBrush}"
                        Width="{TemplateBinding mahapps:ControlsHelper.HeaderFontSize, Converter={StaticResource MetroTabItemCloseButtonWidthConverter}}"
                        Height="{Binding RelativeSource={RelativeSource Self}, Path=Width, Mode=OneWay}"
                        VerticalAlignment="Top"
                        IsTabStop="False"
                        Command="{TemplateBinding CloseTabCommand}"
                        CommandParameter="{TemplateBinding CloseTabCommandParameter}">
                    <Button.OpacityMask>
                        <VisualBrush Stretch="Uniform" 
                                        Visual="{StaticResource iconX}" />
                    </Button.OpacityMask>
                </Button>

                <Button Style="{DynamicResource ChromelessButtonStyle}"
                        Visibility="{TemplateBinding helpers:CControlsHelper.Watched, Converter={StaticResource boolToVisibilityConverter}}"
                        Background="{DynamicResource GrayNormalBrush}"
                        Width="{TemplateBinding mahapps:ControlsHelper.HeaderFontSize, Converter={StaticResource MetroTabItemCloseButtonWidthConverter}}"
                        Height="{Binding RelativeSource={RelativeSource Self}, Path=Width, Mode=OneWay}"
                        VerticalAlignment="Bottom"
                        IsTabStop="False">
                    <Button.OpacityMask>
                        <VisualBrush Stretch="Uniform" 
                                        Visual="{StaticResource iconRefresh}" />
                    </Button.OpacityMask>
                </Button>
            </Grid>
            <Rectangle Width="1" VerticalAlignment="Stretch" Margin="10,5,0,0" Stroke="{DynamicResource AccentColorBrush}" />
        </StackPanel>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected"
                    Value="true">
            <Setter Property="TextElement.Foreground"
                    TargetName="ContentSite"
                    Value="{DynamicResource AccentColorBrush}" />
        </Trigger>
        <Trigger Property="IsSelected"
                    Value="false">
            <Setter Property="TextElement.Foreground"
                    TargetName="ContentSite"
                    Value="{DynamicResource GrayNormalBrush}" />
        </Trigger>
        <Trigger SourceName="ContentSite"
                    Property="IsMouseOver"
                    Value="True">
            <Setter Property="TextElement.Foreground"
                    TargetName="ContentSite"
                    Value="{DynamicResource GrayHoverBrush}" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>  

In Mahapps.Metro 1.2.4 it works well, the PART_CloseButton closes the tab.
In the 1.5.0 the close "X" button stops working. When I see the mahapps code I realized, that there is an InternalCloseCommand (internal), which closes the tab.
When I try to bound a new command handler to close the tab manually, I realized that the TabControl.CloseThisTabItem method is internal too.

My question is:
How can I close the tab with close button when I override the template?

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants