博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF自定义TabControl样式
阅读量:5752 次
发布时间:2019-06-18

本文共 5597 字,大约阅读时间需要 18 分钟。

原文:

WPF自定义TabControl,TabControl美化

XAML代码:

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{    ///     /// TabControl控件封装    ///     public partial class TabControlEx : TabControl    {        ///         /// TabItem右键菜单源        ///         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
tabItemList = new List
(); 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 ///
/// 关闭TabItem /// 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

效果图:

 

转载地址:http://rjukx.baihongyu.com/

你可能感兴趣的文章
php图片赋值,php如何优雅地赋值
查看>>
【探索HTML5第二弹01】HTML5的前世今生以及来世
查看>>
Failed to connect to remote VM. Connection refused. Connection refused: connect
查看>>
freeze
查看>>
SAP HANA存储过程结果视图调用
查看>>
设计模式 ( 十八 ):State状态模式 -- 行为型
查看>>
OracleLinux安装说明
查看>>
nova分析(7)—— nova-scheduler
查看>>
Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
查看>>
OpenMediaVault 搭建git,ssh无法连接问题
查看>>
java多线程之:Java中的ReentrantLock和synchronized两种锁定机制的对比 (转载)
查看>>
【Web动画】SVG 实现复杂线条动画
查看>>
使用Wireshark捕捉USB通信数据
查看>>
Apache Storm 官方文档 —— FAQ
查看>>
iOS 高性能异构滚动视图构建方案 —— LazyScrollView
查看>>
Java 重载、重写、构造函数详解
查看>>
【Best Practice】基于阿里云数加·StreamCompute快速构建网站日志实时分析大屏
查看>>
【云栖大会】探索商业升级之路
查看>>
HybridDB实例新购指南
查看>>
C语言及程序设计提高例程-35 使用指针操作二维数组
查看>>