当前位置:网站首页>WPF中TabControl动态获取当前选中的TabItem
WPF中TabControl动态获取当前选中的TabItem
2022-07-31 12:08:00 【Monkey_King_GL】
前言
TabControl组件是WPF中一个比较强大的组件了,他可以在一个窗口中实现多个页面来回切换。但很多时候我们需要根据选中的TabItem显示另外一部分的页面,这时我们就需要动态获取当前TabItem了。
一、添加事件
我们需要在xaml文件中添加改变事件SelectionChanged = “SelectionChanged”,代码如下:
<TabControl x:Name="Control" SelectionChanged="SelectionChanged" HorizontalAlignment="Left" Height="760" Margin="5" VerticalAlignment="Top" Width="560" TabStripPlacement="Left">
二、添加事件响应
代码如下:
public void SelectionChanged(object sender,SelectionChangedEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() =>
{
var data = this.Control.SelectedItem as TabItem;
var datas = data.Header; //主要是在后端获取到当前的TabItem的Heade
MessageBox.Show("content:" + this.Control.SelectedContent + "; index = " + this.Control.SelectedIndex + "; datas" + datas);
}));
}
此处需要异步操作,如果不使用异步操作,在第一次加载窗口时TabControl还未加载,这样会导致你命名的TabControl是个空值,无法对其进行操作。
总结
我们通过该方式就可以实现在切换TabControl的同时对页面其他部分进行修改,极大地提高了页面的灵活度。
参考:https://www.cnblogs.com/fishyues/p/10076489.html
边栏推荐
- 栈和队列的基本概念
- CWE4.8 -- 2022年危害最大的25种软件安全问题
- 初识QEMU
- cesium-Web网页优化进阶
- Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
- Exploring Plain Vision Transformer Backbones for Object Detection 论文阅读笔记
- 安装MYSQL遇到问题:write configuration file卡主
- Use Excel to read data exposed by SAP ABAP CDS View through ODBC
- JVS轻应用的组成与配置
- 一周精彩内容分享(第14期)
猜你喜欢

Acwing第 62 场周赛【未完结】

Docker installs canal and mysql for simple testing and achieves cache consistency between redis and mysql

IDEA configure method annotation automatic parameters

使用docker搭建mysql主从

After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again

MySQL日志中“binlog”的三种格式玩起来真爽

MySQL模糊查询性能优化

The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.

无法将“node.exe”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。

Read through the interface to call the artifact RestTemplate
随机推荐
busybox之reboot命令流程分析
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
Use jOOQ to write vendor-agnostic SQL with JPA's native query or @Formula.
Exploring Plain Vision Transformer Backbones for Object Detection 论文阅读笔记
Getting started with jmeter performance testing steps (performance testing tool jmeter)
JVS应用中心
Json和对象之间转换的封装(Gson)
学习笔记 Golang 写入文件(io.WriteString、ioutil.WriteFile、file.Write、write.WriteString)
一文吃透接口调用神器RestTemplate
R语言:文本(字符串)处理与正则表达式
JVM 运行时数据区与JMM 内存模型详解
消息队列面试题(2022最新整理)
Addition logic for SAP Commerce Cloud Product Review
CWE4.8 -- The 25 most damaging software security issues in 2022
Detailed tutorial on distributed transaction Seata
MySQL index usage and optimization
串的基本概念与操作
Is the working process of the belt you know the story - actionreducerstore
Use ODBC in Excel to read data from CDS view on SAP BTP platform
VBA输出日志到工作簿demo