WPF之TabControl控件用法
先创建实体基类: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));
}
}
}
}
创建员工类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");
}
}
}
}
}
创建部门类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<Employee> employees; public ObservableCollection<Employee> Employees
{
get
{
if (employees==null)
{
employees = new ObservableCollection<Employee>();
}
return employees;
} } }
}
主窗口的XAML头部引用名称空间:
xmlns:local="clr-namespace:TabControlDemo"
本例中TabControl控件中的TabItem用DataGrid控件来显示数据,
主窗口的资源中定义DataGridCell的样式资源:Key为dgCellStyle,使数据在单元格中居中显示:
<Style x:Key="dgCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
主窗口的资源中定义TabControl控件中的TabItem的样式:
<DataTemplate DataType="{x:Type local:Department}">
<Grid>
<DataGrid ItemsSource="{Binding Path=Employees}" AutoGenerateColumns="False" GridLinesVisibility="All" SelectionMode="Single" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="姓名" Binding="{Binding Path=EmployeeName}" CellStyle="{StaticResource ResourceKey=dgCellStyle}" />
<DataGridTextColumn Header="性别" Binding="{Binding Sex}" CellStyle="{StaticResource ResourceKey=dgCellStyle}" />
<DataGridTextColumn Header="年龄" Binding="{Binding Age}" CellStyle="{StaticResource ResourceKey=dgCellStyle}" />
<DataGridTextColumn Header="职位" Binding="{Binding Title}" CellStyle="{StaticResource ResourceKey=dgCellStyle}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</DataTemplate>
主窗口的XAML完整代码:
<Window x:Class="TabControlDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabControlDemo"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}" Loaded="Window_Loaded">
<Window.Resources>
<Style x:Key="dgCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <DataTemplate DataType="{x:Type local:Department}">
<Grid>
<DataGrid ItemsSource="{Binding Path=Employees}" AutoGenerateColumns="False" GridLinesVisibility="All" SelectionMode="Single" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="姓名" Binding="{Binding Path=EmployeeName}" CellStyle="{StaticResource ResourceKey=dgCellStyle}" />
<DataGridTextColumn Header="性别" Binding="{Binding Sex}" CellStyle="{StaticResource ResourceKey=dgCellStyle}" />
<DataGridTextColumn Header="年龄" Binding="{Binding Age}" CellStyle="{StaticResource ResourceKey=dgCellStyle}" />
<DataGridTextColumn Header="职位" Binding="{Binding Title}" CellStyle="{StaticResource ResourceKey=dgCellStyle}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid Margin="5">
<TabControl ItemsSource="{Binding Path=Departments}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
</Grid>
</Window>
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
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
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<Department> departments; public ObservableCollection<Department> Departments
{
get
{
if (departments == null)
{
departments = new ObservableCollection<Department>();
}
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=,Title="IT部部门经理"});
d1.Employees.Add(new Employee() { EmployeeName = "李四", Sex = "男", Age = , Title = "高级工程师" });
d1.Employees.Add(new Employee() { EmployeeName = "王五", Sex = "男", Age =, Title = "软件工程师" });
d1.Employees.Add(new Employee() { EmployeeName = "小丽", Sex = "女", Age = , Title = "助理工程师" }); Department d2 = new Department { Name = "采购部" };
d2.Employees.Add(new Employee() { EmployeeName = "孙钱", Sex = "男", Age = , Title = "采购部部门经理" });
d2.Employees.Add(new Employee() { EmployeeName = "胡言", Sex = "男", Age = , Title = "采购员" });
d2.Employees.Add(new Employee() { EmployeeName = "梁雨", Sex = "女", Age = , Title = "采购员" }); Department d3 = new Department { Name = "销售部" };
d3.Employees.Add(new Employee() { EmployeeName = "刘明", Sex = "男", Age = , Title = "销售部部门经理" });
d3.Employees.Add(new Employee() { EmployeeName = "霍奇", Sex = "男", Age = , Title = "销售员" });
d3.Employees.Add(new Employee() { EmployeeName = "何军", Sex = "女", Age = , Title = "销售员" }); this.Departments.Add(d1);
this.Departments.Add(d2);
this.Departments.Add(d3);
} }
}
运行效果:



WPF之TabControl控件用法的更多相关文章
- WPF 自定义TabControl控件样式
一.前言 程序中经常会用到TabControl控件,默认的控件样式很普通.而且样式或功能不一定符合我们的要求.比如:我们需要TabControl的标题能够居中.或平均分布:或者我们希望TabContr ...
- TabControl控件用法图解
1.首先创建一个MFC对话框框架,在对话框资源上从工具箱中添加上一个TabControl控件 2.根据需要修改一下属性,然后右击控件,为这个控件添加一个变量,将此控件跟一个CTabCtrl类变量绑定在 ...
- WPF 中RichTextBox控件用法细讲
1. 取得已被选中的内容:(1)使用RichTextBox.Document.Selection属性(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text ...
- WPF中TabControl控件和ListBox控件的简单应用(MVVM)
本文主要实现下图所示的应用场景: 对于Class1页,会显示用户的age和address属性,对于Class2页,会显示用户的age,address和sex属性.在左边的ListBox中选择对应的用户 ...
- Visual Studio中的TabControl控件的用法
今天遇到了一个自己没遇到过的控件TabControl控件,所以找了点关于它的资料 TabControl属性 DisplayRect:只定该控件客户区的一个矩形 HotTrack:设置当鼠标经过页标签 ...
- WPF TabControl控件-事件相关问题
TabControl控件的TabItem的Content元素,例如:DataGrid控件,在对事件的处理时,需要对事件的源引起关注,当需要处理DataGrid的事件时,事件会传递到TabControl ...
- WPF 调用WinForm控件
WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...
- WPF 滚动文字控件MarqueeControl
原文:WPF 滚动文字控件MarqueeControl WPF使用的滚动文字控件,支持上下左右滚动方式,支持设置滚动速度 XAML部分: <UserControl x:Class="U ...
- 深入探讨WPF的ListView控件
接上一篇博客初步探讨WPF的ListView控件(涉及模板.查找子控件) 我们继续探讨ListView的用法 一.实现排序功能 需求是这样的:假如我们把学生的分数放入ListView,当我 ...
随机推荐
- Office在线预览及PDF在线预览的实现方式大集合
一.服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览 微软方:利用Office2007以上版本的一个PDF插件SaveAsPDFandXPS.exe可以导出PDF文件,然后再利用免 ...
- Java中的Math类的简单实用
System.out.println(Math.PI);//获取PI的值 System.out.println(Math.E);//常量E int min = Math.min(5, 4);//求最小 ...
- JS复习:第三章&第四章
第三章 一.把一个值转换成字符串的两种方法: 1.使用每个值都有的toString( )方法.这个方法唯一要做的就是返回相应值的字符串表现.例如: var age = 11 ; var ageAsSt ...
- HDU 1907 John(取火柴博弈2)
传送门 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int ...
- 前台javascript排序
<script type="text/javascript"> $(function () { $('.Sorthead-ShowUp').click(function ...
- RPM安装gcc gcc-c++扩展
rpm -ivh cpp--.el5.i386.rpm 回车 rpm -ivh kernel-headers--.el5.i386.rpm 回车 rpm -ivh glibc-headers-.i38 ...
- samba连接提示“找不到网络路径”
在运行里面输入services.msc,打开服务管理界面. 根据服务的描述,找到了TCP/IP NetBIOS Helper服务描述中提到TCP/IP NetBIOS Helper服务能使用户能够共享 ...
- 管理MySQL的命令
USE 数据库名 :选择要操作的Mysql数据库,使用该命令后所有Mysql命令都只针对该数据库. SHOW DATABASES: 列出 MySQL 数据库管理系统的数据库列表. SHOW TABLE ...
- easyui-datagrid加载时的效率低下,解决方案
在360浏览器中 使用easyui datagrid 加载数据是效率低下,有时候会出现卡机. 可以修改easyui源码进行解决.
- 解决安装WordPress主题及插件需要输入FTP问题
http://www.zhanghenglei.com/wordpress-ftp-update/ 使用Wordpress程序架构的网站如果需要在网站后台升级.安装主题或者插件的时候,总是会提示需要我 ...