C# WPF TabControl控件用法详解

概述

     TabControl我之前有讲过一节,内容详见:C# WPF TabControl用法指南(精品),上节主要讲解了tabcontrol控件的左右翻页,以及页面筛选,以及数据绑定等内容,这节内容继续接续上节内容进行扩展讲解,主要针对页面删除、增加以及对应的事件进行讲解.

功能演示

1dbdeedb85cd81a1c9150b301cbf7d7d.gif

代码实现

前台XAML:

<UserControl x:Class="Caliburn.Micro.Hello.TabControlView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"xmlns:local="clr-namespace:Caliburn.Micro.Hello"xmlns:cal="http://www.caliburnproject.org"mc:Ignorable="d"   d:DesignHeight="450" d:DesignWidth="800" ><Grid ><Grid.RowDefinitions><RowDefinition Height="35"/><RowDefinition Height="Auto"/></Grid.RowDefinitions><StackPanel Grid.Row="0" Margin="2" Orientation="Horizontal" HorizontalAlignment="Right"><Label Content="跳转到页:" VerticalAlignment="Center" Margin="5"/><TextBox Text="{Binding PageIndex}"   VerticalAlignment="Center" Margin="5" MinWidth="50"/><dx:SimpleButton Content="跳转" Name="Button_Click" Margin="5"/></StackPanel><dx:DXTabControl   Grid.Row="1"  SelectedIndex="{Binding SelectedIndex}"ItemsSource="{Binding ParamItems}" Margin="5"cal:Message.Attach="[Event TabAdding]=[DXTAB_TabAdding($source,$eventArgs)];[Event TabRemoved]=[DXTabControl_TabRemoved($source,$eventArgs)];[Event TabRemoving]=[DXTabControl_TabRemoving($source,$eventArgs)];[Event TabHiding]=[DXTabControl_TabHiding($source,$eventArgs)];" ><dx:DXTabControl.ItemHeaderTemplate><DataTemplate><StackPanel Orientation="Horizontal"><!--<Image Source="{Binding ImageLabel}"/>--><dxlc:LayoutItem Label="{Binding Header}"/></StackPanel></DataTemplate></dx:DXTabControl.ItemHeaderTemplate><dx:DXTabControl.ItemTemplate><DataTemplate><ContentControl  cal:View.Model="{Binding SubView}" /></DataTemplate></dx:DXTabControl.ItemTemplate><dx:DXTabControl.View><dx:TabControlScrollView AllowHideTabItems="True" AllowAnimation="True" NewButtonShowMode="InTabPanel" ShowHeaderMenu="True"AllowKeyboardNavigation="True" AllowScrollOnMouseWheel="True" RemoveTabItemsOnHiding="True" /></dx:DXTabControl.View></dx:DXTabControl><!--<dxg:GridControl ><dxg:GridControl.View><dxg:TableView AllowPaging="True"/></dxg:GridControl.View></dxg:GridControl>--></Grid>
</UserControl>

这里绑定了几个事件:

cal:Message.Attach="[Event TabAdding]=[DXTAB_TabAdding($source,$eventArgs)];[Event TabRemoved]=[DXTabControl_TabRemoved($source,$eventArgs)];[Event TabRemoving]=[DXTabControl_TabRemoving($source,$eventArgs)];[Event TabHiding]=[DXTabControl_TabHiding($source,$eventArgs)];" >

TabAdding,是在页面添加前触发,TabRemoved:页面移除完成后触发;TabRemoving:页面移除前触发,TabHiding:页面隐层前触发.

需要注意的是需要触发移除页面事件,首先需要将属性RemoveTabItemsOnHiding设置为true。

NewButtonShowMode这个属性:是个枚举量,设置的是添加页面+按钮的位置,详解如下:

//// 摘要://     Lists values that specify where to show the New button.[Flags]public enum NewButtonShowMode{//// 摘要://     The New Button is not shown.NoWhere = 0,//// 摘要://     The New Button is shown in the Header Area.InHeaderArea = 1,//// 摘要://     The New Button is located inside the Tab Panel, next to tab item headers.InTabPanel = 2,//// 摘要://     The New Button is shown in the Tab Panel and Header Area simultaneously.InHeaderAreaAndTabPanel = 3}

后台cs代码:

using DevExpress.Xpf.Core;
using PropertyChanged;
using System.Collections.ObjectModel;
using System.Windows;namespace Caliburn.Micro.Hello
{[AddINotifyPropertyChangedInterface]public class TabControlViewModel : Screen, IViewModel{public ObservableCollection<ParamDTO> ParamItems { get; set; } = new ObservableCollection<ParamDTO>();public int SelectedIndex { get; set; }public int PageIndex { get; set; }public TabControlViewModel(){DisplayName = "TabControlTest";PageIndex = 3;ParamItems.Add(new ParamDTO() { Header = "1", SubView = new MemorandumViewModel() });ParamItems.Add(new ParamDTO() { Header = "2", SubView = new MemorandumViewModel() });ParamItems.Add(new ParamDTO() { Header = "3", SubView = new MemorandumViewModel() });ParamItems.Add(new ParamDTO() { Header = "4", SubView = new MemorandumViewModel() });}public void Button_Click(){SelectedIndex = PageIndex - 1;}public void DXTAB_TabAdding(object sender, TabControlTabAddingEventArgs e){int currentPageCount = ParamItems.Count;e.Item = new ParamDTO(){Header = $"{currentPageCount + 1}",SubView = new MemorandumViewModel()};}public void DXTabControl_TabRemoving(object sender, TabControlTabRemovingEventArgs e){}public void DXTabControl_TabRemoved(object sender, TabControlTabRemovedEventArgs e){for (int i = 0; i < ParamItems.Count; i++){ParamItems[i].Header = $"{i + 1}";}}public void DXTabControl_TabHiding(object sender, TabControlTabHidingEventArgs e){if (ParamItems.Count == 1){MessageBox.Show($"There must be at least one of ParamDTO pages", "Page Remove");e.Cancel = true;}else{var confirmResult = MessageBox.Show($"确定删除页面","警告",MessageBoxButton.YesNo);if (confirmResult == MessageBoxResult.Yes || confirmResult == MessageBoxResult.OK){//to do}else{e.Cancel = true;}}}}[AddINotifyPropertyChangedInterface]public class ParamDTO{/// <summary>/// 标题/// </summary>public string Header { get; set; }/// <summary>/// SubView/// </summary>public MemorandumViewModel SubView { get; set; }}}

这里在TabHiding的事件里面设置了弹窗确认,如果点了确认再删除页面,点击了取消则不删除,防止了用户误操作把页面删除.页面删除完成后在TabRemoved里面重新排布了页面标题序号.

源码下载:百度网盘

链接:https://pan.baidu.com/s/1LVLSb9PzOme9m0S5GSP6Ow 

提取码:6666

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

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

相关文章

pixel 解锁_如何在Google Pixel 4和Pixel 4 XL上禁用面部解锁

pixel 解锁Justin Duino贾斯汀杜伊诺(Justin Duino)Face Unlock is one of the Google Pixel 4 and Pixel 4 XL’s flagship features. But if the facial recognition is a form of biometric security you’re uncomfortable with, you can delete your face data right off …

【实战】将多个不规则多级表头的工作表合并为一个规范的一维表数据结果表...

最近在项目里&#xff0c;有个临时的小需求&#xff0c;需要将一些行列交叉结构的表格进行汇总合并&#xff0c;转换成规范的一维表数据结构进行后续的分析使用。从一开始想到的使用VBA拼接字符串方式&#xff0c;完成PowerQuery的M语言查询字符串&#xff0c;然后转换成使用插…

happiness[国家集训队2011(吴确)]

【试题来源】 2011中国国家集训队命题答辩【问题描述】 高一一班的座位表是个n*m的矩阵&#xff0c;经过一个学期的相处&#xff0c;每个同学和前后左右相邻的同学互相成为了好朋友。这学期要分文理科了&#xff0c;每个同学对于选择文科与理科有着自己的喜悦值&#xff0c;而一…

sketch怎么移动图层_什么是Photoshop Express,Fix,Mix和Sketch移动应用程序?

sketch怎么移动图层Adobe’s approach to mobile apps seems to be “The More, The Better”. Right now, there are five Photoshop branded apps available for iOS and Android. Adobe的移动应用程序方法似乎是“越多越好”。 目前&#xff0c;有五个适用于iOS和Android的P…

imessage_如何在iPhone和iPad上的iMessage组中提及某人

imessageKhamosh PathakKhamosh PathakSometimes, it’s difficult to get someone’s attention in a large iMessage group chat on your iPhone or iPad. However, if you mention that person specifically in a message, your friend will receive a notification about i…

点击右侧导航栏,实现iframe嵌入子页面中div,滑动到最上面

2019独角兽企业重金招聘Python工程师标准>>> // 点击对应的nav里的li标签,页面就滚动到哪里 $(.title-list > li).click(function(event) {$(this).addClass(active).siblings().removeClass(active);//li标签里面有a标签,可以阻止到a标签的默认行为event.preven…

wepack环境配置1之node的安装

.向往已久的webpack终于配好了.. 1.要安装webpack&#xff0c;首先需要安装nodejs nodejs下载地址:https://nodejs.org/en/ 下载完成后,一步步安装即可,我是安装到D盘 新建一个nodejs的文件夹,装到这个文件夹里面即可. 安装完毕后检查自己是否安装成功.启动cmd,然后输入npm -v,…

【赏析】.NET跨平台框架-Avalonia UI

这是Avalonia UI官方的一个Demo&#xff0c;站长对部分Nuget包进行了升级&#xff0c;网友【小飞机MLA】对Linux版本修复了字体Bug得以正常运行、演示&#xff1a;Windows 11&#xff1a;macOS 13&#xff1a;可安装Rider&#xff08;EAP即要&#xff09;开发&#xff0c;站长一…

office自定义安装选项_如何自定义Office 2013中功能区上的现有选项卡

office自定义安装选项The Ribbon in Microsoft Office 2013 provides quick access to many features and options by default, but it can be further customized to fit the way you use it. You can add a custom tab to the ribbon or you can add commands to the existin…

Centos6.8 安装spark-2.3.1 以及 scala-2.12.2

一、Spark概述 Spark 是一个用来实现快速而通用的集群计算的平台。 在速度方面&#xff0c;Spark 扩展了广泛使用的 MapReduce 计算模型&#xff0c;而且高效地支持更多计算模式&#xff0c;包括交互式查询和流处理。 在处理大规模数据集时&#xff0c;速度是非常重要的。速…

聊一聊 WPF 程序的键盘是如何被窃听的?

一&#xff1a;背景 1.讲故事前几天群里很热闹&#xff0c;看了下在争论两个问题&#xff1a;电脑里要不要装杀毒软件 ?应该装什么杀毒软件 ?不管杀毒软件流氓不流氓&#xff0c;在如今病毒肆虐的当下互联网&#xff0c;装一个还是能帮我们拦截很多意想不到的东西&#xff0c…

httpclient 实现文件上传中转

开发功能&#xff1a; web前端提交上传文件 —> a服务器接收 —> 转发到b服务器进行文件处理 下面是简单实现的代码&#xff0c;具体细节优化根本自己的需求更改。 public String handleResponse(HttpServletRequest request, HttpServletResponse response)throws Unsup…

AngularJS $watch 性能杀手

双向绑定是AngularJS核心概念之一&#xff0c;它给我们带来了思维的转变&#xff0c;不再是以DOM为驱动&#xff0c;而是以Model为核心&#xff0c;View中写上声明式标签&#xff08;指令或{{}}&#xff09;,AngularJS会在后台默默同步View到Model,并将Model的变化更新到View。…

ipad和iphone切图_如何在iPhone和iPad上的Messages App中固定对话

ipad和iphone切图Khamosh PathakKhamosh PathakBetween updates from your bank and group chats, the Messages app on your iPhone or iPad can be a mess. Use the pinned conversations feature introduced in iOS 14 and iPadOS 14 to access your favorite conversations…

这个WPF的企业级MES项目爆火,就是UI争议大!

工业4.0时代&#xff0c;智能智造MES系统大行其道&#xff0c;然而基于.NET跨平台的罕见&#xff01;这里有一套《.NET6WPF企业级MES实战》教程&#xff0c;基于.NET6跨平台开发&#xff0c;实现了MES多核心功能&#xff0c;尤其是开发框架完整&#xff0c;非常适合复用。这里分…

单调栈学习笔记

线性结构——单调栈①定义&#xff1a;栈内的元素&#xff0c;按照某种方式排序&#xff08;单调递增或单调递减&#xff09;如果新入栈的元素破坏了单调性&#xff0c;就弹出栈内元素&#xff0c;直到满足单调性②优点&#xff1a;可以很方便地求出某个数左边或者右边第一个比…

《VMware Virtual SAN权威指南(原书第2版)》一1.5 什么是Virtual SAN

1.5 什么是Virtual SAN Virtual SAN是VMware推出的一种存储解决方案&#xff0c;它的beta版本在2013年发布&#xff0c;2014年3月正式开放给公众&#xff0c;并于2016年3月升级到6.2版。VSAN完全集成在vSphere中&#xff0c;它是一种基于对象的存储系统&#xff0c;是虚拟机存…

在Outlook 2007中查看您的Google日历

Google Calendar is a phenomenal web application for managing your calendars, but so many of us are still forced to use Outlook at work. The good thing is you can have the best of both worlds by subscribing to your Google Calendar from Outlook. Google日历是…

元宇宙、数字孪生和企业NFT

昨天参加了华为云上海开发者日活动&#xff0c;并客串主持了一场"元宇宙技术创新和商业实践之路"的闭门研讨会。研讨会上大家讨论热烈&#xff0c;干货多多&#xff0c;大家提到元宇宙的企业级前景、数字藏品和数字人案例的亲身体会。在会上盆盆分享了自己关于企业级…

CMD命令硬盘/光驱挂载

使用Mountvol命令挂载时&#xff0c;发现GUID不对啊&#xff0c;哪应该到哪找呢&#xff1f; 1.首先可以用Mountvol命令&#xff1a; Mountvol 创建、删除或列出卷的装入点。Mountvol 是一种不需要驱动器号而连接卷的方式。 语法&#xff1a; mountvol [Drive:]Path VolumeName…