当前位置:网站首页>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
边栏推荐
- Different lower_case_table_names settings for server ('1') and data dictionary ('0') solution
- In PLC communication error or timeout or download the prompt solution of the model
- Android studio connects to MySQL and completes simple login and registration functions
- 第十二章 使用中的 OpenAPI 属性
- 0x80070570文件或目录损坏且无法删除(0x80070091怎么删除)
- JVS函数公式使用场景介绍
- 多线程学习笔记-2.final关键字和不变性
- Structural controllability of switched linear systems with symmetry constraints
- St. Regis Takeaway Project: New dishes and dishes paged query
- Distributed id solution
猜你喜欢
随机推荐
The latest MySql installation teaching, very detailed
Quickly learn database management
MySQL模糊查询性能优化
[Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process
Android studio connects to MySQL and completes simple login and registration functions
JS列表数据通过递归实现树形结构
建情人节表白网站(超详细过程,包教包会)
栈和队列的基本概念
普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
第十二章 使用中的 OpenAPI 属性
file contains vulnerabilities
Use Excel to read data exposed by SAP ABAP CDS View through ODBC
Distributed id solution
vb.net 画曲线
Detailed tutorial on distributed transaction Seata
cesium-Web网页优化进阶
Docker installs canal and mysql for simple testing and achieves cache consistency between redis and mysql
The most complete phpmyadmin vulnerability summary
数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
Different lower_case_table_names settings for server ('1') and data dictionary ('0') solution