博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF之TabControl控件用法
阅读量:6758 次
发布时间:2019-06-26

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

先创建实体基类:NotificationObject(用来被实体类继承) 实现属性更改通知接口:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;namespace TabControlDemo{    public class NotificationObject:INotifyPropertyChanged    {        public event PropertyChangedEventHandler PropertyChanged;        public void OnPropertyChanged(string propertyName)        {            if (this.PropertyChanged!=null)            {                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));            }        }    }}
View Code

 

创建员工类Employee继承NotificationObject类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TabControlDemo{    public class Employee:NotificationObject    {        private string employeeName;        public string EmployeeName        {            get { return employeeName; }            set            {                if (value!=employeeName)                {                    employeeName = value;                    OnPropertyChanged("EmployeeName");                }            }        }        private string sex;        public string Sex        {            get { return sex; }            set            {                if (value != sex)                {                    sex = value;                    OnPropertyChanged("Sex");                }            }        }        private int age;        public int Age        {            get { return age; }            set            {                if (value != age)                {                    age = value;                    OnPropertyChanged("Age");                }            }        }        private string title;        public string Title        {            get { return title; }            set            {                if (value != title)                {                    title = value;                    OnPropertyChanged("Title");                }            }        }    }}
View Code

 

创建部门类Department继承NotificationObject类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections.ObjectModel;namespace TabControlDemo{   public class Department:NotificationObject    {        private string name;        public string Name        {            get { return name; }            set             {                if (value!=name)                {                    name = value;                    OnPropertyChanged("Name");                }            }        }        private ObservableCollection
employees; public ObservableCollection
Employees { get { if (employees==null) { employees = new ObservableCollection
(); } return employees; } } }}
View Code

 

主窗口的XAML头部引用名称空间:

xmlns:local="clr-namespace:TabControlDemo"

 

本例中TabControl控件中的TabItem用DataGrid控件来显示数据,

主窗口的资源中定义DataGridCell的样式资源:Key为dgCellStyle,使数据在单元格中居中显示:

View Code

 

主窗口的资源中定义TabControl控件中的TabItem的样式:

View Code

 

主窗口的XAML完整代码:

View Code

 

C#代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;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;using System.ComponentModel;using System.Collections.ObjectModel;namespace TabControlDemo{    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window,INotifyPropertyChanged    {        public event PropertyChangedEventHandler PropertyChanged;        public void OnPropertyChanged(string propertyName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));            }        }        private ObservableCollection
departments; public ObservableCollection
Departments { get { if (departments == null) { departments = new ObservableCollection
(); } return departments; } } public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { Department d1 = new Department { Name="IT部"}; d1.Employees.Add(new Employee() { EmployeeName="张三",Sex="男",Age=30,Title="IT部部门经理"}); d1.Employees.Add(new Employee() { EmployeeName = "李四", Sex = "男", Age = 28, Title = "高级工程师" }); d1.Employees.Add(new Employee() { EmployeeName = "王五", Sex = "男", Age =23, Title = "软件工程师" }); d1.Employees.Add(new Employee() { EmployeeName = "小丽", Sex = "女", Age = 19, Title = "助理工程师" }); Department d2 = new Department { Name = "采购部" }; d2.Employees.Add(new Employee() { EmployeeName = "孙钱", Sex = "男", Age = 30, Title = "采购部部门经理" }); d2.Employees.Add(new Employee() { EmployeeName = "胡言", Sex = "男", Age = 28, Title = "采购员" }); d2.Employees.Add(new Employee() { EmployeeName = "梁雨", Sex = "女", Age = 23, Title = "采购员" }); Department d3 = new Department { Name = "销售部" }; d3.Employees.Add(new Employee() { EmployeeName = "刘明", Sex = "男", Age = 30, Title = "销售部部门经理" }); d3.Employees.Add(new Employee() { EmployeeName = "霍奇", Sex = "男", Age = 28, Title = "销售员" }); d3.Employees.Add(new Employee() { EmployeeName = "何军", Sex = "女", Age = 23, Title = "销售员" }); this.Departments.Add(d1); this.Departments.Add(d2); this.Departments.Add(d3); } }}
View Code

 

 

运行效果:

 

 

转载于:https://www.cnblogs.com/527289276qq/p/5329181.html

你可能感兴趣的文章
Ceres-Solver库入门
查看>>
悲惨而又丢人的创业经历:草根创业者含恨倾诉为什么失败
查看>>
理解WebKit和Chromium: WebKit, WebKit2, Chromium和Chrome介绍
查看>>
hanoi塔的递归算法
查看>>
C# 校验给定的ip地址是否合法
查看>>
lumen 登陆 注册 demo
查看>>
基于服务的并行系统的通讯方式探讨
查看>>
设计模式——观察者模式
查看>>
Python多线程 简明例子
查看>>
《地球上的星星》
查看>>
mysql数据库的主从同步,实现读写分离
查看>>
89 fcanf和fprintf
查看>>
javascript——自定义右键菜单
查看>>
求二叉树中相差最大的两个节点间的差值绝对值
查看>>
PHP 类名::class含义
查看>>
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test
查看>>
小括号匹配
查看>>
设计模式简介和分类,重点在总结
查看>>
数据库默认端口
查看>>
从输入网址到显示网页的全过程分析【转】
查看>>