先创建实体基类: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控件用法的更多相关文章

  1. WPF 自定义TabControl控件样式

    一.前言 程序中经常会用到TabControl控件,默认的控件样式很普通.而且样式或功能不一定符合我们的要求.比如:我们需要TabControl的标题能够居中.或平均分布:或者我们希望TabContr ...

  2. TabControl控件用法图解

    1.首先创建一个MFC对话框框架,在对话框资源上从工具箱中添加上一个TabControl控件 2.根据需要修改一下属性,然后右击控件,为这个控件添加一个变量,将此控件跟一个CTabCtrl类变量绑定在 ...

  3. WPF 中RichTextBox控件用法细讲

    1. 取得已被选中的内容:(1)使用RichTextBox.Document.Selection属性(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text ...

  4. WPF中TabControl控件和ListBox控件的简单应用(MVVM)

    本文主要实现下图所示的应用场景: 对于Class1页,会显示用户的age和address属性,对于Class2页,会显示用户的age,address和sex属性.在左边的ListBox中选择对应的用户 ...

  5. Visual Studio中的TabControl控件的用法

    今天遇到了一个自己没遇到过的控件TabControl控件,所以找了点关于它的资料 TabControl属性 DisplayRect:只定该控件客户区的一个矩形  HotTrack:设置当鼠标经过页标签 ...

  6. WPF TabControl控件-事件相关问题

    TabControl控件的TabItem的Content元素,例如:DataGrid控件,在对事件的处理时,需要对事件的源引起关注,当需要处理DataGrid的事件时,事件会传递到TabControl ...

  7. WPF 调用WinForm控件

    WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...

  8. WPF 滚动文字控件MarqueeControl

    原文:WPF 滚动文字控件MarqueeControl WPF使用的滚动文字控件,支持上下左右滚动方式,支持设置滚动速度 XAML部分: <UserControl x:Class="U ...

  9. 深入探讨WPF的ListView控件

    接上一篇博客初步探讨WPF的ListView控件(涉及模板.查找子控件)  我们继续探讨ListView的用法      一.实现排序功能 需求是这样的:假如我们把学生的分数放入ListView,当我 ...

随机推荐

  1. 第六十一节,html超链接和路径

    html超链接和路径 学习要点:     1.超链接的属性     2.相对与绝对路径     3.锚点设置                              本章主要探讨HTML5中文本元素 ...

  2. Effective JavaScript :第一章

    第一章 一.严格模式与非严格模式 1.在程序中启用严格模式的方式是在程序的最开始增加一个特定的字符串字面量: ‘use strict’ 同样可以在函数体的开始处加入这句指令以启用该函数的严格模式. f ...

  3. POJ 2368 Buttons(巴什博弈变形)

    题目链接 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; ...

  4. Adobe Acrobat Pro 9破解

    (转载,Window8.1/64bit系统亲测可用) 1.删除C:\Program Files\Common Files\Adobe\Adobe PCD\cache\cache.db和C:\Docum ...

  5. WireShark 抓取Telnet包

    用Python的Asyncore.dispatcher写了个小服务器,客户端使用telnet连接上去之后一直显示连接丢失,想抓下包看看 抓包结果如下: 服务器在192.168.1.102:8080 端 ...

  6. 解决VM安装VMTools后错误提示,实现文件共享

    在VM里给Red Hat 9.0安装VMTools后重启,在系统启动过程中出现三处提示,分别为:第一处:Mounting local filesystem: Error: Cannot mount f ...

  7. PHP文本路径转换为链接文字

    <?php /** * 文本路径转换为有链接的文字 * @param string $str 转换内容 * @return string */ function urlToLink($str) ...

  8. pc app 桌面打包

    进入 http://nwjs.io/  下载 创建web项目,在项目根目录 创建文件package.json并填写 1 2 3 4 5 6 7 {   "name": " ...

  9. tableviewcell滑动显示多个按钮UITableViewRowAction(转载)

    demo截图 ios8 新的属性 typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { UITableViewRowActionStyleDe ...

  10. 2.按要求编写Java应用程序: (1)编写西游记人物类(XiYouJiRenWu) 其中属性有:身高(height),名字(name),武器(weapon) 方法有:显示名字(printName),显示武器(printWeapon) (2)在主类的main方法中创建二个对象:zhuBaJie,sunWuKong。并分别为他 们的两个属性(name,weapon)赋值,最后分别调用printNam

    XiYouJiRenWu package com.hanqi.test; public class XiYouJiRenWu { String height,name,weapon; XiYouJiR ...