How to programmatically create a header template for TabControl?

I have a tabcontrol on the main page. I want to add a close button to the tab template header template. How to add title template in tabitems in c # code. please, help..

+3
source share
3 answers

See this: enter these codes into UserControl resources

<Style TargetType='sdk:TabItem'>
        <Setter Property='HeaderTemplate'>
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation='Horizontal'
                                            Background='Transparent'>
                        <TextBlock Text='{Binding}' />
                        <!--<Button Command="{Binding RemoveItemCommand}" VerticalAlignment='Center'
                                        Style="{StaticResource CloseButton}"
                                        Margin="5,0,0,0"
                                        Content="M0,0 L6,6 M6, 0 L0,6"
                                        ToolTipService.ToolTip="Remove item" />-->
                        <Button x:Name='btnCloaseTab'
                                        Click='btnCloaseTab_Click'
                                        VerticalAlignment='Center'
                                        Style="{StaticResource CloseButton}"
                                        Margin="5,0,0,0"
                                        Content="M0,0 L6,6 M6, 0 L0,6"
                                        ToolTipService.ToolTip="بستن زبانه" />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
+1
source

Take a look at the following link that explains how to customize the tab control to add more to the tab. I would add an image button to the tab title and bind a click event to close it.

http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightTabControl07022008170702PM/SilverlightTabControl.aspx

Here is an example

<Grid x:Name="LayoutRoot" Background="White">
    <sdk:TabControl Height="100" HorizontalAlignment="Left" Margin="108,94,0,0" Name="tabControl1" VerticalAlignment="Top" Width="200">
        <sdk:TabItem Name="tabItem1">
            <sdk:TabItem.Header>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Polygon" Margin="1,1,1,1" VerticalAlignment="Center" />
                    <Button Content="X" Click="Button_Click"/>
                </StackPanel>
            </sdk:TabItem.Header>
            <Grid />
        </sdk:TabItem>
    </sdk:TabControl>
</Grid>
0
<Style x:Key="CloseButton"
                 TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent"
                                Width="14"
                                Height="14">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                                                     Storyboard.TargetName="FocusEllipse">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ColorAnimation Duration="0"
                                                                        To="#FFDC3030"
                                                                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                                        Storyboard.TargetName="FocusEllipse" />
                                        <ColorAnimation Duration="0"
                                                                        To="White"
                                                                        Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"
                                                                        Storyboard.TargetName="path" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                                                     Storyboard.TargetName="FocusEllipse">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ColorAnimation Duration="0"
                                                                        To="Black"
                                                                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                                        Storyboard.TargetName="FocusEllipse" />
                                        <ColorAnimation Duration="0"
                                                                        To="White"
                                                                        Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"
                                                                        Storyboard.TargetName="path" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled" />
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused" />
                                <VisualState x:Name="Unfocused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Ellipse x:Name="FocusEllipse"
                                         Fill="#FFF13535"
                                         Visibility="Collapsed" />
                        <Path x:Name="path"
                                    Data="{TemplateBinding Content}"
                                    Stroke="#FF898888"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    StrokeThickness="1" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
0

Source: https://habr.com/ru/post/1773800/


All Articles