先创建实体基类: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. 使用php创建WebSocket服务

    执行方法:首先先修改server.php与index.html的ip通过命令行执行 [php路径]php.exe "[文件路径]server.php"然后通过浏览器打开index. ...

  2. block的内部实现原理

    一.简单定义 block是一个指向结构体的指针,编译器将block内部代码生成对应的函数,上述结构体中的函数指针(funcPtr)指向该函数的实现: 二.相关概念 形参和实参 形参:形式参数,用于定义 ...

  3. maven项目构建

    Maven是apache的一个开源项目.是一个用来把源代码构建成可发布的构件的工具. Maven的功能非常强大,可以认为是一个项目管理工具,不仅仅是一个构建工具. Maven本身的核心很小,但是可以在 ...

  4. hdu_5790_Prefix(trie+主席树)

    题目链接:hdu_5790_Prefix 题意: 给你n个字符串,字符串总长度不超过10W,然后给你一个区间,问你这个区间的字符串不相同的前缀有多少个. 题解: 由于z与上一个答案有关,所以强制在线, ...

  5. 应用 Valgrind 发现 Linux 程序的内存问题(转)

    Valgrind 概述 体系结构 Valgrind 是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个 ...

  6. shp文件显示

    开发环境 Win7, VS2010 Sp1 QGIS 2.01 #include <qgsapplication.h> #include <qgsproviderregistry.h ...

  7. HDU 2037 今年暑假不AC(贪心)

    今年暑假不AC Problem Description “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了 ...

  8. java 类型转换(摘自网络)

    java基本类型转换规则   1.基本数据类型的转换是指由系统根据转换规则自动完成,不需要程序员明确地声明不同数据类型之间的转换.     转换在编译器执行,而不是等到运行期再执行. 2.基本数据类型 ...

  9. shell 之awk 关联数组高级应用

    最近由于数据迁移过,有些用户信息需要再次确认下,也许数据量比较大,但是需要最终确认的比如说是用户ID和其对应的用户积分数,这样就会导致出现文本a(老的数据),文本b(新的数据).比如 这是文本a.tx ...

  10. NOIP2010-普及组复赛-第二题-接水问题

    题目描述 Description 学校里有一个水房,水房里一共装有 m 个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为 1.  现在有 n 名同学准备接水,他们的初始接水顺序已经确定.将这些 ...