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,当我 ...
随机推荐
- NDK常见错误
1.错误1: android mk文件没有定义 $ ndk-buildAndroid NDK: Your APP_BUILD_SCRIPT points to an unknown file: /cy ...
- 【定位:PDF文件定位关键字所在坐标和页码】
iText简介: iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件 ...
- 大数据时代之hadoop(一):hadoop安装
1.hadoop版本介绍 0.20.2版本以前(不含该版本)的配置文件都在default.xml中. 0.20.x以后的版本不含有eclipse插件的jar包,由于eclipse的版本不一,所以就需要 ...
- 【LeetCode】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- javascript表单操作
通过遍历获得列表中被勾选的元素 $("#singcms-push").click(function(){ var id = $("#select-push"). ...
- Graph Algorithm
1.定义 A graph consists of a set of vertices V and a set of edges E. Each edge is a pair (v, w), where ...
- web页面开发相关基础
CSS是一种用于web的标准布局语言,可以控制版面.颜色以及元素和图像的大小和位置.HTML文档应该利用外部样式表来定义文档中使用的样式.JavaScript也应该放在外部文档中,这个文档应该只包含J ...
- tftp常用命令
root@hbg:/# tftpBusyBox v1.22.1 (2015-12-18 15:33:52 CST) multi-call binary. Usage: tftp [OPTIONS] H ...
- Nimbus<一>Storm系列(五)架构分析之Nimbus启动过程
启动流程图 mk-assignments 功能:对当前集群中所有Topology进行新一轮的任务调度. 实现源码路径: \apache-storm-0.9.4\storm-core\src\clj\b ...
- Excel教程(6) - 外部函数
EUROCONVERT 用途:将数字转换为欧元形式,将数字由欧元形式转换为 欧盟成员国货币形式,或利用欧元作为中间货币将数字由某一 欧盟成员国货币转化为另一欧盟成员国货币的形式(三角转换 关系). 语 ...