精华内容
下载资源
问答
  • 自定义TabControl

    2015-07-27 16:45:40
    自定义TabControl 拖拽 关闭按钮 自定义背景色 tab名称双击修改。
  • 自定义tabcontrol

    2013-09-18 10:39:46
    vs自带的控件外观实在不敢恭维,客户老是反映界面太老土了,自己重绘了一个TabControl,效果不错,给大家分享一下。
  • C#自定义 tabcontrol

    2017-12-27 16:24:56
    自定义 TabControl 改变样式 ,有关闭小叉叉 ,可以点击关闭
  • WPF 自定义TabControl

    2014-09-18 17:03:53
    C# WPF 自定义TabControl TanItem过多时可左右滑动 可删除
  • 仿苹果Safari浏览器选项卡自定义TabControl控件
  • WPF自定义TabControl样式

    2019-04-02 11:38:00
    WPF自定义TabControl样式 原文:WPF自定义TabControl样式WPF自定义TabControl,TabControl美化 XAML代码: <TabControl x:Class="SunCreate.Common.Controls.TabControlEx" xmlns="http...
    原文: WPF自定义TabControl样式

    WPF自定义TabControl,TabControl美化

    XAML代码:

    <TabControl x:Class="SunCreate.Common.Controls.TabControlEx"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:SunCreate.Common.Controls"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300" SelectionChanged="TabControl_SelectionChanged" >
        <TabControl.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                </ResourceDictionary.MergedDictionaries>
                <!--菜单样式-->
                <ControlTemplate x:Key="menuTemplate" TargetType="ContextMenu">
                    <Border Name="bd" Background="#99001133">
                        <ItemsPresenter/>
                    </Border>
                </ControlTemplate>
                <ControlTemplate x:Key="menuSeperatorTemplate" TargetType="Separator">
                    <Border Background="#6fff">
                    </Border>
                </ControlTemplate>
                <ControlTemplate x:Key="menuItemTemplate" TargetType="MenuItem">
                    <Border Name="bd" Height="30" Background="Transparent">
                        <StackPanel Orientation="Horizontal">
                            <Image x:Name="img" Stretch="None" Margin="10,0,10,0" Source="/SunCreate.Common.Controls;Component/Images/Controls/二级菜单左箭头.png"></Image>
                            <TextBlock x:Name="tb" Margin="0,0,10,0" Foreground="#fff" VerticalAlignment="Center" Text="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="bd" Property="Background" Value="#99001133" />
                            <Setter TargetName="tb" Property="Foreground" Value="#ff5e5e" />
                            <Setter TargetName="tb" Property="Margin" Value="0,0,9,0" />
                            <Setter TargetName="img" Property="Source" Value="/SunCreate.Common.Controls;Component/Images/Controls/左箭头_选中.png"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </ResourceDictionary>
        </TabControl.Resources>
        <TabControl.Template>
            <ControlTemplate TargetType="TabControl" >
                <ControlTemplate.Resources>
                    <Style TargetType="TabItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="TabItem">
                                    <Grid x:Name="gridTabItem" Tag="{Binding ElementName=tabCloseBtn}" HorizontalAlignment="Center" MouseLeftButtonDown="tabItem_MouseLeftButtonDown" MouseRightButtonUp="tabItem_MouseRightButtonUp" >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition></ColumnDefinition>
                                            <ColumnDefinition></ColumnDefinition>
                                            <ColumnDefinition Width="16"></ColumnDefinition>
                                            <ColumnDefinition></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Path x:Name="pathLeft" Height="4" Width="5" Data="M 0,4 L 5,4 5,0 C 5,0 5,4 0,4 Z" Fill="#096691" StrokeThickness="0" VerticalAlignment="Bottom" Visibility="Collapsed" SnapsToDevicePixels="True" >
                                        </Path>
                                        <Path x:Name="pathRight" Height="4" Width="5" Data="M 0,0 L 0,4 5,4 C 5,4 0,4 0,0 Z" Grid.Column="3" Fill="#096691" StrokeThickness="0" VerticalAlignment="Bottom" Visibility="Collapsed" SnapsToDevicePixels="True" >
                                        </Path>
                                        <Border x:Name="bdText" Grid.Column="1" Margin="0 0 0 0" Background="#096691" CornerRadius="3 0 0 0" SnapsToDevicePixels="True" >
                                            <TextBlock x:Name="txt" Margin="15 0 10 0" FontSize="12" Foreground="#fff" FontFamily="微软雅黑,黑体" Text="{TemplateBinding Header}" VerticalAlignment="Center"></TextBlock>
                                        </Border>
                                        <Border x:Name="bdBtn" Grid.Column="2" Margin="0 0 0 0" Background="#096691" CornerRadius="0 3 0 0" SnapsToDevicePixels="True" >
                                        </Border>
                                        <Button x:Name="btnTabItemClose" Grid.Column="2" Width="7" Height="7" HorizontalAlignment="Right" Click="btnTabItemClose_Click" VerticalAlignment="Top"  Margin="0,5,5,0">
                                            <Button.Template>
                                                <ControlTemplate TargetType="{x:Type Button}">
                                                    <Border Background="Transparent">
                                                        <Image Stretch="Fill" x:Name="imgTabClose" Source="/SunCreate.Common.Controls;Component/Images/Controls/菜单关闭.png" ></Image>
                                                    </Border>
                                                    <ControlTemplate.Triggers>
                                                        <Trigger Property="IsPressed" Value="true">
                                                            <Setter TargetName="imgTabClose"  Property="Margin" Value="1"></Setter>
                                                        </Trigger>
                                                        <Trigger Property="IsMouseOver" Value="true">
                                                            <Setter TargetName="imgTabClose"  Property="Margin" Value="1"></Setter>
                                                        </Trigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Button.Template>
                                        </Button>
                                    </Grid>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsSelected" Value="true">
                                            <Setter TargetName="bdText" Property="Background" Value="#012f3f"></Setter>
                                            <Setter TargetName="bdBtn" Property="Background" Value="#012f3f"></Setter>
                                            <Setter TargetName="pathLeft" Property="Fill" Value="#012f3f"></Setter>
                                            <Setter TargetName="pathRight" Property="Fill" Value="#012f3f"></Setter>
                                            <Setter TargetName="pathLeft" Property="Visibility" Value="Visible"></Setter>
                                            <Setter TargetName="pathRight" Property="Visibility" Value="Visible"></Setter>
                                            <Setter TargetName="gridTabItem" Property="Margin" Value="0 0 -8 0"></Setter>
                                        </Trigger>
                                        <Trigger Property="IsSelected" Value="false">
                                            <Setter TargetName="gridTabItem" Property="Margin" Value="5 0 -3 0"></Setter>
                                            <Setter TargetName="txt" Property="Foreground" Value="#78a7c1"></Setter>
                                        </Trigger>
                                        <Trigger Property="IsMouseOver" Value="true">
                                            <Setter TargetName="txt" Property="Foreground" Value="#f2f5f7"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ControlTemplate.Resources>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="26"></RowDefinition>
                        <RowDefinition Height="1*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Border>
                        <StackPanel MinWidth="{TemplateBinding Property=ActualWidth}" Orientation="Horizontal" Margin="2,0,0,0" IsItemsHost="True"></StackPanel>
                    </Border>
                    <Border Grid.Row="1" Background="#012f3f" CornerRadius="2" >
                        <ContentPresenter  Content="{TemplateBinding Property=SelectedContent }">
                        </ContentPresenter>
                    </Border>
                </Grid>
            </ControlTemplate>
        </TabControl.Template>
        <TabControl.ContextMenu>
            <ContextMenu Name="menu" Template="{StaticResource menuTemplate}">
                <MenuItem Header="关闭标签" Template="{StaticResource menuItemTemplate}" CommandParameter="0" Click="menuItemClick"></MenuItem>
                <Separator Height="1" Template="{StaticResource menuSeperatorTemplate}" Margin="1 0 1 0"></Separator>
                <MenuItem Header="关闭其他标签" Template="{StaticResource menuItemTemplate}" CommandParameter="1" Click="menuItemClick"></MenuItem>
                <MenuItem Header="关闭左侧标签" Template="{StaticResource menuItemTemplate}" CommandParameter="2" Click="menuItemClick"></MenuItem>
                <MenuItem Header="关闭右侧标签" Template="{StaticResource menuItemTemplate}" CommandParameter="3" Click="menuItemClick"></MenuItem>
            </ContextMenu>
        </TabControl.ContextMenu>
    </TabControl>
    View Code

    C#代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace SunCreate.Common.Controls
    {
        /// <summary>
        /// TabControl控件封装
        /// </summary>
        public partial class TabControlEx : TabControl
        {
            /// <summary>
            /// TabItem右键菜单源
            /// </summary>
            private TabItem _contextMenuSource;
    
            public TabControlEx()
            {
                InitializeComponent();
            }
    
            private void tabItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
    
            }
    
            private void tabItem_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
            {
                _contextMenuSource = (sender as Grid).TemplatedParent as TabItem;
                this.menu.PlacementTarget = sender as Grid;
                this.menu.Placement = PlacementMode.MousePoint;
                this.menu.IsOpen = true;
            }
    
            #region TabItem右键菜单点击事件
            private void menuItemClick(object sender, RoutedEventArgs e)
            {
                MenuItem btn = e.Source as MenuItem;
                int data = Convert.ToInt32(btn.CommandParameter.ToString());
    
                if (_contextMenuSource != null)
                {
                    List<TabItem> tabItemList = new List<TabItem>();
                    if (data == 0)
                    {
                        tabItemList.Add(_contextMenuSource);
                    }
                    if (data == 1)
                    {
                        for (int i = 0; i < this.Items.Count; i++)
                        {
                            TabItem tabItem = this.Items[i] as TabItem;
                            if (tabItem != _contextMenuSource)
                            {
                                tabItemList.Add(tabItem);
                            }
                        }
                    }
                    if (data == 2)
                    {
                        for (int i = 0; i < this.Items.Count; i++)
                        {
                            TabItem tabItem = this.Items[i] as TabItem;
                            if (tabItem != _contextMenuSource)
                            {
                                tabItemList.Add(tabItem);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (data == 3)
                    {
                        for (int i = this.Items.Count - 1; i >= 0; i--)
                        {
                            TabItem tabItem = this.Items[i] as TabItem;
                            if (tabItem != _contextMenuSource)
                            {
                                tabItemList.Add(tabItem);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    foreach (TabItem tabItem in tabItemList)
                    {
                        CloseTabItem(tabItem);
                    }
                }
            }
            #endregion
    
            private void btnTabItemClose_Click(object sender, RoutedEventArgs e)
            {
                var btn = sender as Button;
                var tmplParent = (btn.Parent as Grid).TemplatedParent;
                var tabItem = tmplParent as TabItem;
                CloseTabItem(tabItem);
            }
    
            #region 关闭TabItem
            /// <summary>
            /// 关闭TabItem
            /// </summary>
            private void CloseTabItem(TabItem tabItem)
            {
                if (tabItem.Content is WorkSpaceContent)
                {
                    var content = tabItem.Content as WorkSpaceContent;
                    if (content != null)
                    {
                        content.Disposed();
                    }
                    tabItem.Content = null;
                    content = null;
                }
                this.Items.Remove(tabItem);
            }
            #endregion
    
            private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                foreach (TabItem tabItem in e.RemovedItems)
                {
                    Panel.SetZIndex(tabItem, 99);
                }
                foreach (TabItem tabItem in e.AddedItems)
                {
                    Panel.SetZIndex(tabItem, 999);
                }
            }
    
        }
    }
    View Code

    效果图:

     

    posted on 2019-04-02 11:38 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

    转载于:https://www.cnblogs.com/lonelyxmas/p/10641641.html

    展开全文
  • 自定义TabControl的Item显示样式
  • 自定义TabControl控件源码 功能介绍: 自定义TabControl控件,界面美观源码完整。 对控件制作有兴趣的可以参考学习下。
  • 主要给大家介绍了关于WPF如何自定义TabControl控件样式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
  • 自定义TabControl的Item的显示样式
  • 自定义TabControl的Item的显示样式
  • vs2010自定义tabcontrol C#源代码vs2010自定义tabcontrol C#源代码
  • WPF 自定义TabControl控件样式 原文:WPF 自定义TabControl控件样式一、前言 程序中经常会用到TabControl控件,默认的控件样式很普通。而且样式或功能不一定符合我们的要求。比如:我们需要TabControl...
    原文: WPF 自定义TabControl控件样式

    一、前言

    程序中经常会用到TabControl控件,默认的控件样式很普通。而且样式或功能不一定符合我们的要求。比如:我们需要TabControl的标题能够居中、或平均分布;或者我们希望TabControl的标题能够进行关闭。要实现这些功能我们需要对TabControl的样式进行定义。

    二、实现TabControl的标题平均分布

    默认的TabControl标题是使用TabPanel容器包含的。要想实现TabControl标题头平均分布,需要把TabPanel替换成UniformGrid;

    替换后的TabControl样式如下:

     <Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
                <Setter Property="Padding" Value="2"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Background" Value="White"/>
                <Setter Property="BorderBrush" Value="#FFACACAC"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabControl}">
                            <Grid x:Name="templateRoot" ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition x:Name="ColumnDefinition0"/>
                                    <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                                    <RowDefinition x:Name="RowDefinition1" Height="*"/>
                                </Grid.RowDefinitions>
                                <UniformGrid x:Name="HeaderPanel" Rows="1" Background="Transparent" Grid.Column="0" IsItemsHost="True" Margin="0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                                <Line X1="0" X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Stroke="White" StrokeThickness="0.1" VerticalAlignment="Bottom" Margin="0 0 0 1" SnapsToDevicePixels="True"/>
                                <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                                    <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="TabStripPlacement" Value="Bottom">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                                </Trigger>
                                <Trigger Property="TabStripPlacement" Value="Left">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                                </Trigger>
                                <Trigger Property="TabStripPlacement" Value="Right">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

    即使这样设置了,TabControl的标题还是很丑,这个时候就需要通过设置TabItem来更改标题样式了。

    TabItem样式如下:

    <Style x:Key="TabItemStyle" TargetType="{x:Type TabItem}">
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="#FFACACAC"/>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid x:Name="templateRoot"  SnapsToDevicePixels="True" Background="Transparent">
                                <TextBlock x:Name="txt" Visibility="Visible" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Header}" ToolTip="{TemplateBinding Header}" Foreground="{TemplateBinding Foreground}" TextTrimming="CharacterEllipsis" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                   
                                    <Setter Property="Foreground" TargetName="txt" Value="#fffea1"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
    
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Panel.ZIndex" Value="1"/>
                                    <Setter Property="Foreground" TargetName="txt" Value="#fffea1"/>
                                </MultiDataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

    至此,样式已经设置完毕,引用示例:

       <Grid Background="#858586">
                    <TabControl Style="{StaticResource TabControlStyle}" Width="300" Height="200" Background="Transparent" BorderBrush="Transparent" BorderThickness="0">
                        <TabItem Style="{StaticResource TabItemStyle}" Cursor="Hand" Header="音乐电台" Height="38" >
                            <Grid Background="#33ffffff">
                                <TextBlock Text="音乐电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </TabItem>
                        <TabItem Style="{StaticResource TabItemStyle}" Cursor="Hand" Header="Mv电台" Height="38" >
                            <Grid Background="#33ffffff">
                                <TextBlock Text="Mv电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </TabItem>
                    </TabControl>
                </Grid>

    效果如下:

    三、实现TabControl标题居中显示(不平均分布)

    同理需要更改TabControl的样式和TabItem的样式。需要把使用TabPanel作为标题的容器,设置HorizontalAlignment为Center;

    TabControl的样式如下:

     <Style x:Key="TabControlWithUnderLineStyle" TargetType="{x:Type TabControl}">
            <Setter Property="Padding" Value="2"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="#FFACACAC"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid x:Name="templateRoot" ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="ColumnDefinition0"/>
                                <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                                <RowDefinition x:Name="RowDefinition1" Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel x:Name="HeaderPanel" HorizontalAlignment="Center" Background="Transparent" Grid.Column="0" IsItemsHost="True" Margin="0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                            <Line X1="0" X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Stroke="Gray" StrokeThickness="0.1" VerticalAlignment="Bottom" Margin="0 0 0 1" SnapsToDevicePixels="True"/>
                            <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                                <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="TabStripPlacement" Value="Bottom">
                                <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                                <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                            </Trigger>
                            <Trigger Property="TabStripPlacement" Value="Left">
                                <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                                <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                                <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                            </Trigger>
                            <Trigger Property="TabStripPlacement" Value="Right">
                                <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                                <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                                <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                                <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    TabItem样式如下:

     <Style x:Key="TabItemExWithUnderLineStyle"  TargetType="{x:Type TabItem}">
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="#FFACACAC"/>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid x:Name="templateRoot"  SnapsToDevicePixels="True" Background="Transparent">
                                <Border x:Name="_underline" BorderBrush="#37aefe" BorderThickness="0" Margin="{TemplateBinding Margin}"/>
                                <Grid>
                                    <TextBlock x:Name="txt" Visibility="Visible" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Header}" ToolTip="{TemplateBinding Header}" Foreground="{TemplateBinding Foreground}" TextTrimming="CharacterEllipsis" />
                                </Grid>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
    
                                    <Setter Property="Foreground" TargetName="txt" Value="#37aefe"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                             
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Panel.ZIndex" Value="1"/>
                                    <Setter Property="Foreground" TargetName="txt" Value="#37aefe"/>
                                    <Setter Property="BorderThickness" TargetName="_underline" Value="0 0 0 2"/>
                                </MultiDataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

    引用示例:

    <Grid Background="#858586">
                    <TabControl Style="{StaticResource TabControlWithUnderLineStyle}" Foreground="Black" Width="300" Height="200" Background="Transparent" BorderBrush="Transparent" BorderThickness="0">
                        <TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Cursor="Hand" Header="音乐电台" Height="38" Width="70" Margin="5 0">
                            <Grid Background="#33ffffff">
                                <TextBlock Text="音乐电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </TabItem>
                        <TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Cursor="Hand" Header="Mv电台" Height="38" Width="70" Margin="5 0">
                            <Grid Background="#33ffffff">
                                <TextBlock Text="Mv电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </TabItem>
                    </TabControl>
                </Grid>

    效果如下:

    四、带关闭按钮的TabControl

     带关闭按钮的TabControl其实就是就是扩展TabItem,需要新建WPF自定义控件,命名为TabItemClose吧;

    C#代码如下:

     public class TabItemClose : TabItem
        {
            static TabItemClose()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(TabItemClose), new FrameworkPropertyMetadata(typeof(TabItemClose)));
            }
    
            private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                d.SetValue(e.Property, e.NewValue);
            }
    
            /// <summary>
            /// 是否可以关闭
            /// </summary>
            public bool IsCanClose
            {
                get { return (bool)GetValue(IsCanCloseProperty); }
                set { SetValue(IsCanCloseProperty, value); }
            }
    
            public static readonly DependencyProperty IsCanCloseProperty =
                DependencyProperty.Register("IsCanClose", typeof(bool), typeof(TabItemClose), new PropertyMetadata(true, OnPropertyChanged));
    
            /// <summary>
            /// 关闭的图标
            /// </summary>
            public ImageSource CloseIcon
            {
                get { return (ImageSource)GetValue(CloseIconProperty); }
                set { SetValue(CloseIconProperty, value); }
            }
    
            public static readonly DependencyProperty CloseIconProperty =
                DependencyProperty.Register("CloseIcon", typeof(ImageSource), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
    
            /// <summary>
            /// 正常背景色
            /// </summary>
            public SolidColorBrush NormalBackground
            {
                get { return (SolidColorBrush)GetValue(NormalBackgroundProperty); }
                set { SetValue(NormalBackgroundProperty, value); }
            }
    
            public static readonly DependencyProperty NormalBackgroundProperty =
                DependencyProperty.Register("NormalBackground", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
            /// <summary>
            /// 悬浮背景色
            /// </summary>
            public SolidColorBrush OverBackgound
            {
                get { return (SolidColorBrush)GetValue(OverBackgoundProperty); }
                set { SetValue(OverBackgoundProperty, value); }
            }
    
            public static readonly DependencyProperty OverBackgoundProperty =
                DependencyProperty.Register("OverBackgound", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
            /// <summary>
            /// 选中背景色
            /// </summary>
            public SolidColorBrush SelectedBackgound
            {
                get { return (SolidColorBrush)GetValue(SelectedBackgoundProperty); }
                set { SetValue(SelectedBackgoundProperty, value); }
            }
    
            public static readonly DependencyProperty SelectedBackgoundProperty =
                DependencyProperty.Register("SelectedBackgound", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
            /// <summary>
            /// 默认前景色
            /// </summary>
            public SolidColorBrush NormalForeground
            {
                get { return (SolidColorBrush)GetValue(NormalForegroundProperty); }
                set { SetValue(NormalForegroundProperty, value); }
            }
    
            public static readonly DependencyProperty NormalForegroundProperty =
                DependencyProperty.Register("NormalForeground", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
            /// <summary>
            /// 悬浮前景色
            /// </summary>
            public SolidColorBrush OverForeground
            {
                get { return (SolidColorBrush)GetValue(OverForegroundProperty); }
                set { SetValue(OverForegroundProperty, value); }
            }
    
            public static readonly DependencyProperty OverForegroundProperty =
                DependencyProperty.Register("OverForeground", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
            /// <summary>
            /// 选中前景色
            /// </summary>
            public SolidColorBrush SelectedForeground
            {
                get { return (SolidColorBrush)GetValue(SelectedForegroundProperty); }
                set { SetValue(SelectedForegroundProperty, value); }
            }
    
            public static readonly DependencyProperty SelectedForegroundProperty =
                DependencyProperty.Register("SelectedForeground", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
            /// <summary>
            /// 控件圆角
            /// </summary>
            public CornerRadius CornerRadius
            {
                get { return (CornerRadius)GetValue(CornerRadiusProperty); }
                set { SetValue(CornerRadiusProperty, value); }
            }
    
            public static readonly DependencyProperty CornerRadiusProperty =
                DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(TabItemClose), new PropertyMetadata(new CornerRadius(0), OnPropertyChanged));
    
    
            /// <summary>
            /// 前置Logo
            /// </summary>
            public ImageSource LogoIcon
            {
                get { return (ImageSource)GetValue(LogoIconProperty); }
                set { SetValue(LogoIconProperty, value); }
            }
    
            public static readonly DependencyProperty LogoIconProperty =
                DependencyProperty.Register("LogoIcon", typeof(ImageSource), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
    
            /// <summary>
            /// 前置Logo宽度
            /// </summary>
            public double LogoIconWidth
            {
                get { return (double)GetValue(LogoIconWidthProperty); }
                set { SetValue(LogoIconWidthProperty, value); }
            }
    
            public static readonly DependencyProperty LogoIconWidthProperty =
                DependencyProperty.Register("LogoIconWidth", typeof(double), typeof(TabItemClose), new PropertyMetadata(double.Parse("0"), OnPropertyChanged));
    
    
            /// <summary>
            /// 前置Logo高度
            /// </summary>
            public double LogoIconHeigth
            {
                get { return (double)GetValue(LogoIconHeigthProperty); }
                set { SetValue(LogoIconHeigthProperty, value); }
            }
    
            public static readonly DependencyProperty LogoIconHeigthProperty =
                DependencyProperty.Register("LogoIconHeigth", typeof(double), typeof(TabItemClose), new PropertyMetadata(double.Parse("0"), OnPropertyChanged));
    
    
            /// <summary>
            /// LogoPadding
            /// </summary>
            public Thickness LogoPadding
            {
                get { return (Thickness)GetValue(LogoPaddingProperty); }
                set { SetValue(LogoPaddingProperty, value); }
            }
    
            public static readonly DependencyProperty LogoPaddingProperty =
                DependencyProperty.Register("LogoPadding", typeof(Thickness), typeof(TabItemClose), new PropertyMetadata(new Thickness(0), OnPropertyChanged));
    
            /// <summary>
            /// 关闭item事件
            /// </summary>
            public event RoutedEventHandler CloseItem
            {
                add { AddHandler(CloseItemEvent, value); }
                remove { RemoveHandler(CloseItemEvent, value); }
            }
            public static readonly RoutedEvent CloseItemEvent =
                EventManager.RegisterRoutedEvent("CloseItem", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TabItemClose));
    
            /// <summary>
            /// 关闭项的右键菜单
            /// </summary>
            public ContextMenu ItemContextMenu { get; set; }
    
            Border ItemBorder;
    
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                ItemBorder = Template.FindName("_bordertop", this) as Border;
                if (ItemContextMenu != null)
                {
                    ItemBorder.ContextMenu = ItemContextMenu;
                }
            }
        }

    这里面我们添加了很多扩展功能,包括右键菜单,图标显示和控件圆角,以及各种背景色属性。

    然后为TabItemClose设置样式

     <Style TargetType="{x:Type local:TabItemClose}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="Foreground" Value="#666666"/>
            <Setter Property="Margin" Value="0 0 0 0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="CloseIcon" Value="/Images/close2.png"/>
            <Setter Property="NormalBackground" Value="White"/>
            <Setter Property="OverBackgound" Value="#33ca5100"/>
            <Setter Property="SelectedBackgound" Value="#ca5100"/>
            <Setter Property="NormalForeground" Value="#555558"/>
            <Setter Property="OverForeground" Value="White"/>
            <Setter Property="SelectedForeground" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:TabItemClose}">
                        <Border x:Name="_bordertop"   Width="{TemplateBinding Width}" MaxWidth="{TemplateBinding MaxWidth}" Height="{TemplateBinding Height}" CornerRadius="{TemplateBinding CornerRadius}"  Background="{TemplateBinding NormalBackground}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" ToolTip="{TemplateBinding Header}"  >
                            <DockPanel>
                                <Image x:Name="_logo" DockPanel.Dock="Left" Visibility="Visible" Margin="{TemplateBinding LogoPadding}" Source="{TemplateBinding LogoIcon}" VerticalAlignment="Center"  HorizontalAlignment="Center" Stretch="Uniform" Width="{TemplateBinding LogoIconWidth}" Height="{TemplateBinding LogoIconHeigth}" />
                                <Grid Name="_grid" SnapsToDevicePixels="True">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition x:Name="_col_close" Width="20" />
                                    </Grid.ColumnDefinitions>
                                    <Border Grid.ColumnSpan="2" Background="White" Opacity="0"/>
                                    <TextBlock   x:Name="_txt" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"  Margin="3 0 3 0"   Foreground="{TemplateBinding NormalForeground}" TextAlignment="Center" HorizontalAlignment="Center"   Text="{TemplateBinding Header}"  />
                                    <Grid x:Name="_gridclose" Grid.Column="1"  >
                                        <Border x:Name="_borderbg" Background="Black" Opacity="0" />
                                        <local:ButtonEx x:Name="PART_Close_TabItem" HorizontalAlignment="Center"  VerticalAlignment="Center" Background="Transparent" Visibility="Visible" Icon="{TemplateBinding CloseIcon}"  ButtonType="Icon" />
                                    </Grid>
                                </Grid>
                            </DockPanel>
    
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="LogoIcon" Value="{x:Null}">
                                <Setter TargetName="_logo" Property="Visibility" Value="Collapsed" />
                            </Trigger>
                            <Trigger Property="IsCanClose" Value="false">
                                <Setter TargetName="_gridclose" Property="Visibility" Value="Collapsed"/>
                                <Setter TargetName="_col_close" Property="Width" Value="0"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_bordertop" Property="Background" Value="{Binding SelectedBackgound,RelativeSource={RelativeSource TemplatedParent}}" />
                                <Setter TargetName="_txt"  Property="Foreground" Value="{Binding SelectedForeground,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="true"/>
                                    <Condition Property="IsSelected" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="_txt"  Property="Foreground" Value="{Binding OverForeground,RelativeSource={RelativeSource TemplatedParent}}"/>
                                <Setter TargetName="_bordertop"  Property="Background" Value="{Binding OverBackgound,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    这里面使用了一个close的图标

    TabControl的图标可设置可不设置,看自己需要。

    这里面还用到了前面讲的控件ButtonEx,定义方法我就不重复赘述了。大家可以通过这个链接跳转查看:http://www.cnblogs.com/xiaomingg/p/8699125.html。ButtonEx.cs里面还要添加几个方法用来支持关闭TabItem:

     protected override void OnClick()
            {
                base.OnClick();
    
                if (!string.IsNullOrEmpty(Name) && Name == "PART_Close_TabItem")
                {
                    TabItemClose itemclose = FindVisualParent<TabItemClose>(this);
                    (itemclose.Parent as TabControl).Items.Remove(itemclose);
                    RoutedEventArgs args = new RoutedEventArgs(TabItemClose.CloseItemEvent, itemclose);
                    itemclose.RaiseEvent(args);
                }
    
            }
    
            public static T FindVisualParent<T>(DependencyObject obj) where T : class
            {
                while (obj != null)
                {
                    if (obj is T)
                        return obj as T;
    
                    obj = VisualTreeHelper.GetParent(obj);
                }
    
                return null;
            }

    引用示例:

     <Grid Background="#858586">
                    <TabControl  Foreground="Black" Width="300" Height="200" Background="Transparent" BorderBrush="Transparent" BorderThickness="0">
                        <local:TabItemClose  Cursor="Hand" Header="音乐电台" Height="20"  Width="100">
                            <Grid Background="#aaffffff">
                                <TextBlock Text="音乐电台"  VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </local:TabItemClose>
                        <local:TabItemClose  Cursor="Hand" Header="Mv电台" Height="20" Width="100">
                            <Grid Background="#aaffffff">
                                <TextBlock Text="Mv电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </local:TabItemClose>
                    </TabControl>
                </Grid>

    效果如下:

    所有代码已经上传到github:https://github.com/cmfGit/WpfDemo.git

     

    posted on 2019-04-17 14:15 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

    转载于:https://www.cnblogs.com/lonelyxmas/p/10723229.html

    展开全文
  • WPF自定义TabControl标题滚动显示

    一、TabControl简介

    TabControl控件是WPF中常用的可以切换不同窗口、页面和空间的实现选项卡功能的控件,根据微软文档对它的介绍,继承关系如下
    FrameworkElement——Control——ItemsControl——Selector——TabControl
    TabControl为 ItemsControl ,这意味着它可以包含任何类型的对象的集合 (例如字符串、图像或面板) 。

    二、自定义TabControl控件

    TabControl控件默认的控件样式或功能有时不一定满足我们的需求,这时我们可以对其进行自定义,设置style属性,修改默认值 ControlTemplate。
    有关自定义的详细信息可以浏览微软官方文档,ControlTemplate ,请参阅通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于的部分和状态,请参阅TabControl 样式和模板

    三、自定义TabControl选项滚动条显示

    当给TabControl控件添加多个TabItem时,如果是将选项位置即TabStripPlacement属性设置为Left,那么当数量较多时窗口会无法显示全部选项,这时就需要使用滚动条来显示全部选项。
    在WPF中要实现滚动条显示一般是使用ScrollViewer控件,而TabControl选项即标题区域是使用TabPanel容器包含的,因此思路就是自定义TabControl样式,然后在标题区域外面加上一个ScrollViewer,Xaml实现代码如下:

    <Window.Resources>
            <Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
                <Setter Property="Padding" Value="2"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Background" Value="White"/>
                <Setter Property="BorderBrush" Value="#FFACACAC"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabControl}">
                            <Grid x:Name="templateRoot" ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition x:Name="ColumnDefinition0"/>
                                    <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                                    <RowDefinition x:Name="RowDefinition1" Height="*"/>
                                </Grid.RowDefinitions>
                                <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                                <TabPanel x:Name="HeaderPanel" Background="Transparent" Grid.Column="0" IsItemsHost="True" Margin="0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                                </ScrollViewer>
                                <Line X1="0" X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Stroke="White" StrokeThickness="0.1" VerticalAlignment="Bottom" Margin="0 0 0 1" SnapsToDevicePixels="True"/>
                                <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                                    <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="TabStripPlacement" Value="Bottom">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                                </Trigger>
                                <Trigger Property="TabStripPlacement" Value="Left">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                                </Trigger>
                                <Trigger Property="TabStripPlacement" Value="Right">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
    

    style样式需要在Window.Resources内定义,定义好样式后,在后面添加TabControl控件是需要引用定义的样式,代码如下:

    <TabControl x:Name="ImageTab"  TabStripPlacement="Left" Style="{StaticResource TabControlStyle}"/>
    

    参考文档

    TabControl 类
    WPF 自定义TabControl控件样式
    WPF自定义TabControl样式
    WPF-TabControl 选项卡
    WPF自适应可关闭的TabControl 类似浏览器的标签页

    展开全文
  • wpf 自定义tabcontrol

    2018-06-15 14:52:24
    自定义的wpf实现tabcontrol控件,能够实现tabcontrol控件的所有功能,希望对大家们有所帮助,代码是c#和xaml组合
  • 自定义TabControl.rar

    2019-09-09 14:56:30
    KMenuTabControl.cs / KTabControl.cs 以两种不同美化样式进行TabControl原始控件样式的美化操作
  • WPF 自定义TabControl控件样式(转)

    千次阅读 2018-12-09 15:43:10
    WPF 自定义TabControl控件样式 一、前言 程序中经常会用到TabControl控件,默认的控件样式很普通。而且样式或功能不一定符合我们的要求。比如:我们需要TabControl的标题能够居中、或平均分布;或者我们希望...

    WPF 自定义TabControl控件样式

    一、前言

    程序中经常会用到TabControl控件,默认的控件样式很普通。而且样式或功能不一定符合我们的要求。比如:我们需要TabControl的标题能够居中、或平均分布;或者我们希望TabControl的标题能够进行关闭。要实现这些功能我们需要对TabControl的样式进行定义。

    二、实现TabControl的标题平均分布

    默认的TabControl标题是使用TabPanel容器包含的。要想实现TabControl标题头平均分布,需要把TabPanel替换成UniformGrid;

    替换后的TabControl样式如下:

    复制代码

     <Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
                <Setter Property="Padding" Value="2"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Background" Value="White"/>
                <Setter Property="BorderBrush" Value="#FFACACAC"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabControl}">
                            <Grid x:Name="templateRoot" ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition x:Name="ColumnDefinition0"/>
                                    <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                                    <RowDefinition x:Name="RowDefinition1" Height="*"/>
                                </Grid.RowDefinitions>
                                <UniformGrid x:Name="HeaderPanel" Rows="1" Background="Transparent" Grid.Column="0" IsItemsHost="True" Margin="0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                                <Line X1="0" X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Stroke="White" StrokeThickness="0.1" VerticalAlignment="Bottom" Margin="0 0 0 1" SnapsToDevicePixels="True"/>
                                <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                                    <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="TabStripPlacement" Value="Bottom">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                                </Trigger>
                                <Trigger Property="TabStripPlacement" Value="Left">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                                </Trigger>
                                <Trigger Property="TabStripPlacement" Value="Right">
                                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

    复制代码

    即使这样设置了,TabControl的标题还是很丑,这个时候就需要通过设置TabItem来更改标题样式了。

    TabItem样式如下:

    复制代码

    <Style x:Key="TabItemStyle" TargetType="{x:Type TabItem}">
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="#FFACACAC"/>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid x:Name="templateRoot"  SnapsToDevicePixels="True" Background="Transparent">
                                <TextBlock x:Name="txt" Visibility="Visible" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Header}" ToolTip="{TemplateBinding Header}" Foreground="{TemplateBinding Foreground}" TextTrimming="CharacterEllipsis" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                   
                                    <Setter Property="Foreground" TargetName="txt" Value="#fffea1"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
    
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Panel.ZIndex" Value="1"/>
                                    <Setter Property="Foreground" TargetName="txt" Value="#fffea1"/>
                                </MultiDataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

    复制代码

    至此,样式已经设置完毕,引用示例:

    复制代码

       <Grid Background="#858586">
                    <TabControl Style="{StaticResource TabControlStyle}" Width="300" Height="200" Background="Transparent" BorderBrush="Transparent" BorderThickness="0">
                        <TabItem Style="{StaticResource TabItemStyle}" Cursor="Hand" Header="音乐电台" Height="38" >
                            <Grid Background="#33ffffff">
                                <TextBlock Text="音乐电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </TabItem>
                        <TabItem Style="{StaticResource TabItemStyle}" Cursor="Hand" Header="Mv电台" Height="38" >
                            <Grid Background="#33ffffff">
                                <TextBlock Text="Mv电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </TabItem>
                    </TabControl>
                </Grid>

    复制代码

    效果如下:

    三、实现TabControl标题居中显示(不平均分布)

    同理需要更改TabControl的样式和TabItem的样式。需要把使用TabPanel作为标题的容器,设置HorizontalAlignment为Center;

    TabControl的样式如下:

    复制代码

     <Style x:Key="TabControlWithUnderLineStyle" TargetType="{x:Type TabControl}">
            <Setter Property="Padding" Value="2"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="#FFACACAC"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid x:Name="templateRoot" ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="ColumnDefinition0"/>
                                <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                                <RowDefinition x:Name="RowDefinition1" Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel x:Name="HeaderPanel" HorizontalAlignment="Center" Background="Transparent" Grid.Column="0" IsItemsHost="True" Margin="0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                            <Line X1="0" X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Stroke="Gray" StrokeThickness="0.1" VerticalAlignment="Bottom" Margin="0 0 0 1" SnapsToDevicePixels="True"/>
                            <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                                <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="TabStripPlacement" Value="Bottom">
                                <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                                <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                            </Trigger>
                            <Trigger Property="TabStripPlacement" Value="Left">
                                <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                                <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                                <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                            </Trigger>
                            <Trigger Property="TabStripPlacement" Value="Right">
                                <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                                <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                                <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                                <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                                <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    复制代码

    TabItem样式如下:

    复制代码

     <Style x:Key="TabItemExWithUnderLineStyle"  TargetType="{x:Type TabItem}">
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="#FFACACAC"/>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid x:Name="templateRoot"  SnapsToDevicePixels="True" Background="Transparent">
                                <Border x:Name="_underline" BorderBrush="#37aefe" BorderThickness="0" Margin="{TemplateBinding Margin}"/>
                                <Grid>
                                    <TextBlock x:Name="txt" Visibility="Visible" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Header}" ToolTip="{TemplateBinding Header}" Foreground="{TemplateBinding Foreground}" TextTrimming="CharacterEllipsis" />
                                </Grid>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
    
                                    <Setter Property="Foreground" TargetName="txt" Value="#37aefe"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Left"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Bottom"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Right"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
                                </MultiDataTrigger>
                             
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                                        <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}}" Value="Top"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Panel.ZIndex" Value="1"/>
                                    <Setter Property="Foreground" TargetName="txt" Value="#37aefe"/>
                                    <Setter Property="BorderThickness" TargetName="_underline" Value="0 0 0 2"/>
                                </MultiDataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

    复制代码

    引用示例:

    复制代码

    <Grid Background="#858586">
                    <TabControl Style="{StaticResource TabControlWithUnderLineStyle}" Foreground="Black" Width="300" Height="200" Background="Transparent" BorderBrush="Transparent" BorderThickness="0">
                        <TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Cursor="Hand" Header="音乐电台" Height="38" Width="70" Margin="5 0">
                            <Grid Background="#33ffffff">
                                <TextBlock Text="音乐电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </TabItem>
                        <TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Cursor="Hand" Header="Mv电台" Height="38" Width="70" Margin="5 0">
                            <Grid Background="#33ffffff">
                                <TextBlock Text="Mv电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </TabItem>
                    </TabControl>
                </Grid>

    复制代码

    效果如下:

    四、带关闭按钮的TabControl

     带关闭按钮的TabControl其实就是就是扩展TabItem,需要新建WPF自定义控件,命名为TabItemClose吧;

    C#代码如下:

    复制代码

     public class TabItemClose : TabItem
        {
            static TabItemClose()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(TabItemClose), new FrameworkPropertyMetadata(typeof(TabItemClose)));
            }
    
            private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                d.SetValue(e.Property, e.NewValue);
            }
    
            /// <summary>
            /// 是否可以关闭
            /// </summary>
            public bool IsCanClose
            {
                get { return (bool)GetValue(IsCanCloseProperty); }
                set { SetValue(IsCanCloseProperty, value); }
            }
    
            public static readonly DependencyProperty IsCanCloseProperty =
                DependencyProperty.Register("IsCanClose", typeof(bool), typeof(TabItemClose), new PropertyMetadata(true, OnPropertyChanged));
    
            /// <summary>
            /// 关闭的图标
            /// </summary>
            public ImageSource CloseIcon
            {
                get { return (ImageSource)GetValue(CloseIconProperty); }
                set { SetValue(CloseIconProperty, value); }
            }
    
            public static readonly DependencyProperty CloseIconProperty =
                DependencyProperty.Register("CloseIcon", typeof(ImageSource), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
    
            /// <summary>
            /// 正常背景色
            /// </summary>
            public SolidColorBrush NormalBackground
            {
                get { return (SolidColorBrush)GetValue(NormalBackgroundProperty); }
                set { SetValue(NormalBackgroundProperty, value); }
            }
    
            public static readonly DependencyProperty NormalBackgroundProperty =
                DependencyProperty.Register("NormalBackground", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
            /// <summary>
            /// 悬浮背景色
            /// </summary>
            public SolidColorBrush OverBackgound
            {
                get { return (SolidColorBrush)GetValue(OverBackgoundProperty); }
                set { SetValue(OverBackgoundProperty, value); }
            }
    
            public static readonly DependencyProperty OverBackgoundProperty =
                DependencyProperty.Register("OverBackgound", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
            /// <summary>
            /// 选中背景色
            /// </summary>
            public SolidColorBrush SelectedBackgound
            {
                get { return (SolidColorBrush)GetValue(SelectedBackgoundProperty); }
                set { SetValue(SelectedBackgoundProperty, value); }
            }
    
            public static readonly DependencyProperty SelectedBackgoundProperty =
                DependencyProperty.Register("SelectedBackgound", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
            /// <summary>
            /// 默认前景色
            /// </summary>
            public SolidColorBrush NormalForeground
            {
                get { return (SolidColorBrush)GetValue(NormalForegroundProperty); }
                set { SetValue(NormalForegroundProperty, value); }
            }
    
            public static readonly DependencyProperty NormalForegroundProperty =
                DependencyProperty.Register("NormalForeground", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
            /// <summary>
            /// 悬浮前景色
            /// </summary>
            public SolidColorBrush OverForeground
            {
                get { return (SolidColorBrush)GetValue(OverForegroundProperty); }
                set { SetValue(OverForegroundProperty, value); }
            }
    
            public static readonly DependencyProperty OverForegroundProperty =
                DependencyProperty.Register("OverForeground", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
            /// <summary>
            /// 选中前景色
            /// </summary>
            public SolidColorBrush SelectedForeground
            {
                get { return (SolidColorBrush)GetValue(SelectedForegroundProperty); }
                set { SetValue(SelectedForegroundProperty, value); }
            }
    
            public static readonly DependencyProperty SelectedForegroundProperty =
                DependencyProperty.Register("SelectedForeground", typeof(SolidColorBrush), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
            /// <summary>
            /// 控件圆角
            /// </summary>
            public CornerRadius CornerRadius
            {
                get { return (CornerRadius)GetValue(CornerRadiusProperty); }
                set { SetValue(CornerRadiusProperty, value); }
            }
    
            public static readonly DependencyProperty CornerRadiusProperty =
                DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(TabItemClose), new PropertyMetadata(new CornerRadius(0), OnPropertyChanged));
    
    
            /// <summary>
            /// 前置Logo
            /// </summary>
            public ImageSource LogoIcon
            {
                get { return (ImageSource)GetValue(LogoIconProperty); }
                set { SetValue(LogoIconProperty, value); }
            }
    
            public static readonly DependencyProperty LogoIconProperty =
                DependencyProperty.Register("LogoIcon", typeof(ImageSource), typeof(TabItemClose), new PropertyMetadata(null, OnPropertyChanged));
    
    
    
            /// <summary>
            /// 前置Logo宽度
            /// </summary>
            public double LogoIconWidth
            {
                get { return (double)GetValue(LogoIconWidthProperty); }
                set { SetValue(LogoIconWidthProperty, value); }
            }
    
            public static readonly DependencyProperty LogoIconWidthProperty =
                DependencyProperty.Register("LogoIconWidth", typeof(double), typeof(TabItemClose), new PropertyMetadata(double.Parse("0"), OnPropertyChanged));
    
    
            /// <summary>
            /// 前置Logo高度
            /// </summary>
            public double LogoIconHeigth
            {
                get { return (double)GetValue(LogoIconHeigthProperty); }
                set { SetValue(LogoIconHeigthProperty, value); }
            }
    
            public static readonly DependencyProperty LogoIconHeigthProperty =
                DependencyProperty.Register("LogoIconHeigth", typeof(double), typeof(TabItemClose), new PropertyMetadata(double.Parse("0"), OnPropertyChanged));
    
    
            /// <summary>
            /// LogoPadding
            /// </summary>
            public Thickness LogoPadding
            {
                get { return (Thickness)GetValue(LogoPaddingProperty); }
                set { SetValue(LogoPaddingProperty, value); }
            }
    
            public static readonly DependencyProperty LogoPaddingProperty =
                DependencyProperty.Register("LogoPadding", typeof(Thickness), typeof(TabItemClose), new PropertyMetadata(new Thickness(0), OnPropertyChanged));
    
            /// <summary>
            /// 关闭item事件
            /// </summary>
            public event RoutedEventHandler CloseItem
            {
                add { AddHandler(CloseItemEvent, value); }
                remove { RemoveHandler(CloseItemEvent, value); }
            }
            public static readonly RoutedEvent CloseItemEvent =
                EventManager.RegisterRoutedEvent("CloseItem", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TabItemClose));
    
            /// <summary>
            /// 关闭项的右键菜单
            /// </summary>
            public ContextMenu ItemContextMenu { get; set; }
    
            Border ItemBorder;
    
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                ItemBorder = Template.FindName("_bordertop", this) as Border;
                if (ItemContextMenu != null)
                {
                    ItemBorder.ContextMenu = ItemContextMenu;
                }
            }
        }

    复制代码

    这里面我们添加了很多扩展功能,包括右键菜单,图标显示和控件圆角,以及各种背景色属性。

    然后为TabItemClose设置样式

    复制代码

     <Style TargetType="{x:Type local:TabItemClose}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="Foreground" Value="#666666"/>
            <Setter Property="Margin" Value="0 0 0 0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="CloseIcon" Value="/Images/close2.png"/>
            <Setter Property="NormalBackground" Value="White"/>
            <Setter Property="OverBackgound" Value="#33ca5100"/>
            <Setter Property="SelectedBackgound" Value="#ca5100"/>
            <Setter Property="NormalForeground" Value="#555558"/>
            <Setter Property="OverForeground" Value="White"/>
            <Setter Property="SelectedForeground" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:TabItemClose}">
                        <Border x:Name="_bordertop"   Width="{TemplateBinding Width}" MaxWidth="{TemplateBinding MaxWidth}" Height="{TemplateBinding Height}" CornerRadius="{TemplateBinding CornerRadius}"  Background="{TemplateBinding NormalBackground}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" ToolTip="{TemplateBinding Header}"  >
                            <DockPanel>
                                <Image x:Name="_logo" DockPanel.Dock="Left" Visibility="Visible" Margin="{TemplateBinding LogoPadding}" Source="{TemplateBinding LogoIcon}" VerticalAlignment="Center"  HorizontalAlignment="Center" Stretch="Uniform" Width="{TemplateBinding LogoIconWidth}" Height="{TemplateBinding LogoIconHeigth}" />
                                <Grid Name="_grid" SnapsToDevicePixels="True">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition x:Name="_col_close" Width="20" />
                                    </Grid.ColumnDefinitions>
                                    <Border Grid.ColumnSpan="2" Background="White" Opacity="0"/>
                                    <TextBlock   x:Name="_txt" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"  Margin="3 0 3 0"   Foreground="{TemplateBinding NormalForeground}" TextAlignment="Center" HorizontalAlignment="Center"   Text="{TemplateBinding Header}"  />
                                    <Grid x:Name="_gridclose" Grid.Column="1"  >
                                        <Border x:Name="_borderbg" Background="Black" Opacity="0" />
                                        <local:ButtonEx x:Name="PART_Close_TabItem" HorizontalAlignment="Center"  VerticalAlignment="Center" Background="Transparent" Visibility="Visible" Icon="{TemplateBinding CloseIcon}"  ButtonType="Icon" />
                                    </Grid>
                                </Grid>
                            </DockPanel>
    
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="LogoIcon" Value="{x:Null}">
                                <Setter TargetName="_logo" Property="Visibility" Value="Collapsed" />
                            </Trigger>
                            <Trigger Property="IsCanClose" Value="false">
                                <Setter TargetName="_gridclose" Property="Visibility" Value="Collapsed"/>
                                <Setter TargetName="_col_close" Property="Width" Value="0"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_bordertop" Property="Background" Value="{Binding SelectedBackgound,RelativeSource={RelativeSource TemplatedParent}}" />
                                <Setter TargetName="_txt"  Property="Foreground" Value="{Binding SelectedForeground,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="true"/>
                                    <Condition Property="IsSelected" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="_txt"  Property="Foreground" Value="{Binding OverForeground,RelativeSource={RelativeSource TemplatedParent}}"/>
                                <Setter TargetName="_bordertop"  Property="Background" Value="{Binding OverBackgound,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    复制代码

    这里面使用了一个close的图标

    TabControl的图标可设置可不设置,看自己需要。

    这里面还用到了前面讲的控件ButtonEx,定义方法我就不重复赘述了。大家可以通过这个链接跳转查看:http://www.cnblogs.com/xiaomingg/p/8699125.html。ButtonEx.cs里面还要添加几个方法用来支持关闭TabItem:

    复制代码

     protected override void OnClick()
            {
                base.OnClick();
    
                if (!string.IsNullOrEmpty(Name) && Name == "PART_Close_TabItem")
                {
                    TabItemClose itemclose = FindVisualParent<TabItemClose>(this);
                    (itemclose.Parent as TabControl).Items.Remove(itemclose);
                    RoutedEventArgs args = new RoutedEventArgs(TabItemClose.CloseItemEvent, itemclose);
                    itemclose.RaiseEvent(args);
                }
    
            }
    
            public static T FindVisualParent<T>(DependencyObject obj) where T : class
            {
                while (obj != null)
                {
                    if (obj is T)
                        return obj as T;
    
                    obj = VisualTreeHelper.GetParent(obj);
                }
    
                return null;
            }

    复制代码

    引用示例:

    复制代码

     <Grid Background="#858586">
                    <TabControl  Foreground="Black" Width="300" Height="200" Background="Transparent" BorderBrush="Transparent" BorderThickness="0">
                        <local:TabItemClose  Cursor="Hand" Header="音乐电台" Height="20"  Width="100">
                            <Grid Background="#aaffffff">
                                <TextBlock Text="音乐电台"  VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </local:TabItemClose>
                        <local:TabItemClose  Cursor="Hand" Header="Mv电台" Height="20" Width="100">
                            <Grid Background="#aaffffff">
                                <TextBlock Text="Mv电台" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </local:TabItemClose>
                    </TabControl>
                </Grid>

    复制代码

    效果如下:

    所有代码已经上传到github:https://github.com/cmfGit/WpfDemo.git

    展开全文
  • C#2005自定义TabControl

    2011-11-02 11:01:47
    C# 自定义 美化 TabControl————不要资源分啊,欢迎下载使用!
  • 美化自定义TabControl控件

    热门讨论 2013-01-03 20:01:18
    一个仿win7优化大师界面的TabControl控件控件
  • 首先设置DrawMode 属性tabControl.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;tabControl.DrawItem += new DrawItemEventHandler(this.tabControl_DrawItem);然后tabControl_DrawItem事件中代码...
  • 自定义tabcontrol实现

    2017-02-09 17:01:01
    c# tabcontrol自定义实现具体代码如下所示:using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,051
精华内容 820
关键字:

自定义tabcontrol