WPF自定义TabControl样式

WPF自定义TabControl样式
原文: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);}}}#endregionprivate 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);}#endregionprivate 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/365034.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

11计算机,11-计算机科学与技术

《11-计算机科学与技术》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《11-计算机科学与技术(23页珍藏版)》请在人人文库网上搜索。1、计算机软件技术基础,机械工业出版社2007年7月,第一部分 数据结构与算法基础,第1章 算法 第2章 数据结构的基础 第3章 线性表及其存…

Spring新变化

让我们检查一下Spring社区在前几天宣布的一些新版本&#xff1a; Spring Boot 1.0.0.RC1 Spring很高兴地宣布Spring Boot v1.0.0的第一个候选版本。 它包含了先前里程碑中的许多错误修复和增强功能。 二进制文件已发布到Spring Maven存储库 。 使用下载说明和Maven / Gradle坐…

H.264分层结构与码流结构

H.264分层结构 H.264编码器输出的Bit流中&#xff0c;每个Bit都隶属于某个句法元素。句法元素被组织成有层次的结构&#xff0c;分别描述各个层次的信息。 在H.264 中&#xff0c;句法元素共被组织成 序列、图像、片、宏块、子宏块五个层次。在这样的结构中&#xff0c;每一层…

20个很酷的CSS3导航菜单制作教程

CSS3 是对 CSS 规范的一个很大的改善和增强&#xff0c;它使得 Web 开发人员可以很容易的在网站中加入时尚的效果。以前很多需要编写 JavaScript 才能实现的效果&#xff0c;如今只需要简单的写几句 CSS3 代码就能实现。今天这篇文章就向大家推荐20个很酷的CSS3导航菜单制作教程…

金算盘高手论坛资料中心_3D006期 菜鸟论坛精英PK专栏 速来围观!!

点上方“菜鸟选号论坛”→点右上角“...”→选“星标★”每日上午更新&#xff0c;星标置顶与大神不走散苹果是置顶&#xff0c;安卓是星标 点击"菜鸟选号论坛"关注我们论坛明星版块&#xff0c;集全网各路高手之大乘&#xff0c;打造草根明星 展示舞台。同时主要是为…

模拟最后一堂课

前言 如果您已经阅读了其他有关异常模拟的博客文章&#xff0c;则可以通过此链接跳过前奏 。 我被要求汇总一些示例&#xff0c;这些示例如何模拟因其可测试性问题而众所周知的Java结构&#xff1a; 模拟私人方法 模拟最终方法 模拟最后一堂课 模拟构造器 模拟静态方法 …

ajax 浏览器后退,全站Ajax浏览器后退方法

全站Ajax后退有两种方法&#xff0c;浏览器后退按钮加入历史链接。其中一种是常规的Ajax后退&#xff0c;这种后退是带有ajax效果的&#xff0c;要重新请求历史链接页面。另一种是将历史数据保存&#xff0c;后退时取出缓存的数据即可。后者比前者效率要高&#xff0c;所以速度…

java基础day11---空指针异常----引用类型--自定义类型赋值--封装

day111.空指针异常-----属于运行错误-java.long.NullPointerException--原因&#xff1a;当引用名称的值为null时&#xff0c;就不能访问某个对象中的属性或方法热锅非要访问这就出现空指针异常--解决办法&#xff1a;在访问某个对象中的属性或方法之前必须保证该引用名称中存放…

CentOS 7下安装Python3.6

•安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel •到python官网找到下载路径, 用wget下载 wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz •解压tgz包 tar -zxvf Python-3.6.4.t…

非常酷!10个基于 HTML5 的字体应用演示网站

HTML5 是现在Web开发领域的热点&#xff0c;更多的开发人员开始使用HTML5来开发交互性强、效果出众的网站和各种应用。这是 HTML5 网站大观系列第四篇&#xff0c;本文与大家分享5个非常酷的基于 HTML5 的字体应用演示网站&#xff0c;一起欣赏。 Web Typography For The Lone…

ntp如何确认与服务器偏差_LED电子时钟显示屏如何实现时间同步统一校时?

LED电子时钟显示屏采用一体式铝合金边框设计&#xff0c;更坚固美观节能环保&#xff0c;更以其简单的操作和稳定的性能&#xff0c;广泛应用于学校、考场、医院、金融、移动通信、石油、电力、交通、工业以及国防等同步时钟系统的显示终端&#xff0c;LED电子时钟显示屏已成为…

jquery ajax下拉联动,基于jQuery Ajax实现下拉框无刷新联动

本文实例为大家分享了jQuery Ajax实现下拉框无刷新联动的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下HTML代码&#xff1a;{Layout null;}using DAL;using System.Data;{AreaDal areaDal new AreaDal();string areaId ViewBag.areaId;DataRow drArea areaDal.…

Java 8新闻

Java 8&#xff0c; b128和Java 8 RC2的第一个候选版本于2月出现&#xff0c;修复了新Comparator API中的一个严重缺陷&#xff0c;该缺陷中其新的thenComparing()方法具有不必要的类型限制。 java.util.Comparator的thenComparing()方法不需要类型U扩展java.lang.Comparable &…

HDP 2.6 requires libtirpc-devel

HDP 2.6 requires libtirpc-devel 个问题&#xff0c;截止 Mustafa Kemal MAYUK 2017年06月30日 06:30 hadoopPowerSystemsHello, I am trying to install HDP 2.6 on RHEL 7.2 ppc64le. Installation over ambari fails due to "Error: Package: hadoop_2_6_0_3_8-hdfs-2…

推荐10款非常有用的 Ajax 插件

这篇文章与大家分享的是10款非常有用的 Ajax 插件&#xff0c;有用于图片的&#xff0c;用于分页的&#xff0c;还有用于导航的。这些作者的想法特别新颖&#xff0c;希望你能从中找到自己需要的插件。 1. AJAX-ZOOM 非常强大的一款插件&#xff0c;可用鼠标滚轮进行缩放&…

luogu P1083 借教室

传送门 小水题吧 二分能处理到的询问即可 用差分维护前x个订单之后的值 最后求一遍前缀和 如果爆负就是有不满足的 复杂度O((mn)lgm) 或者区间加和区间最小值线段树也行(常数略大) Code:(线段树) 1 #include<cstdio>2 #include<cstring>3 #include<cmath>4 #…

svgaps绘制时不能用中文命名吗_设计师需要了解的切图命名规范

2020年8月21日下午4点50分 黄河公园通常我们在界面设计完成之后要切图给到前端开发。初做UI设计时&#xff0c;把重点都放在设计效果图上&#xff0c;对之后的切图命名规范没有很注重。当时我会有一些疑惑&#xff0c;切图命名的原则是什么&#xff1f;直接命名为能表达清楚的名…

hdu-1728(贪心bfs的灵活运用吧)

链接 [https://vjudge.net/contest/256476#problem/D] 题意 给定一个m n (m行, n列)的迷宫&#xff0c;迷宫中有两个位置&#xff0c;gloria想从迷宫的一个位置走到另外一个位置&#xff0c;当然迷宫中有些地方是空地&#xff0c;gloria可以穿越&#xff0c;有些地方是障碍&am…

jquery ajax.then,jQuery动态AJAX Promise链

小编典典解决方案使用for&#xff1a;var array [One, Two, Three];var id array[0];var data getData(id);for (var i 1; i < array.length; i) {// Or only the last "i" will be used(function (i) {data data.then(function() {return getData(array[i])…

分享20个漂亮的DIV CSS标准化站点案例

看到有些网站的代码十分优雅&#xff0c;心想自己什么时候也能写出这么好的代码。其实&#xff0c;这不是一朝一夕能达到的&#xff0c;需要的实践中不断锤炼和提高。我们可以通过分析别人写得好的网页代码来更加快速的提高自己&#xff0c;下面向大家推荐20个基于DIV CSS标准布…