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

本文共 5645 字,大约阅读时间需要 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

效果图:

 

转载于:https://www.cnblogs.com/s0611163/p/9994594.html

你可能感兴趣的文章
codewars020: The Clockwise Spiral 数字顺时针螺旋矩阵
查看>>
ios 下拉刷新
查看>>
Django在Windows系统下的安装配置
查看>>
懒到极致:对mybatis的进一步精简
查看>>
Android学习之OTA Update
查看>>
Maven Multi-environment package
查看>>
JMM-java内存模型
查看>>
iOS的soap应用(webservice) 开发
查看>>
Delphi listview 点击列头排序
查看>>
android preference page
查看>>
mysql索引挑选
查看>>
关于冰岛足球的段子
查看>>
在 Windows 中安装 Laravel 5.1.X
查看>>
TeamViewer 9发布-在Linux下安装运行
查看>>
Centos7 Gitea安装教程 - 一款易搭建,运行快的Git服务器
查看>>
CentOS minimal 网络配置
查看>>
Nginx架构
查看>>
为什么结构体中的数组不能用const int变量指定大小?
查看>>
模板特化疑问
查看>>
ruby多线程理解
查看>>