Search code examples
silverlightxamlcontrol-template

Silverlight 4 : Making Closeable Tabitems


I would like to extend the tab control to have closeable tab items.

I have found this WPF solution of Kent: On the WPF TabControl - can I add content next to the tab headers?

I opened a copy of the existing silverlight tabcontrol in Blend. However the structure looks quite different to the WPF tabcontrol. I can't get it right into the Silverlight control template.

Does anyone know a good resource for me?


Solution

  • You can Template TabItem to have some sort of close button that you can hook up in code behind to close the currently selected tab.

    <Style TargetType="TabItem">
                <Setter.Value>
                    <ControlTemplate TargetType="sdk:TabItem">
                                <Button x:Name="PART_btnClose"
                                                Height="15"
                                                Width="15"
                                                Grid.Column="1"
                                                HorizontalAlignment="Right"
                                                VerticalAlignment="Center"
                                                Margin="20,0,3,8" BorderThickness="1" Cursor="Hand" />
    </ControlTemplate>
    </Setter.Value>
    </Style>
    

    After this, in on apply template you can subscribe to the ButtonClicked Event.

    Something like this:

    public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
    
            PART_btnClose = GetTemplateChild("PART_btnClose") as Button;
    
            if (PART_btnClose != null)
            {
                PART_btnClose.Click += new RoutedEventHandler(PART_btnClose_Click);
            }
    

    In that event, you can close your tab.

    Hope This helps, code might not work as is, just did it quickly.

    Ty Rozak