WPF DataGrid 示例 应用源码

界面运行效果:

XMAL 源码:

<Window x:Class="WpfApp1.UI.DataGridExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:local="clr-namespace:WpfApp1.UI"
xmlns:assembly="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="DataGridExample" Height="782" Width="800">

<Window.Resources >
<ObjectDataProvider x:Key="sexEnum" MethodName="GetValues" ObjectType="{x:Type assembly:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="local:SexOpt"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<!--设置日期列的显示方式-->
<DataTemplate x:Key="DateTemplate">

<StackPanel Width="40" Height="30">
<Border Background="Orange" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding ExamDate,StringFormat={}{0:MM-dd}}" FontSize="10" HorizontalAlignment="Center"></TextBlock>
</Border>
</StackPanel>

</DataTemplate>
<!--设置日期列的编辑方式-->
<DataTemplate x:Key="EditingDateTemplate">
<DatePicker SelectedDate="{Binding ExamDate}"></DatePicker>
</DataTemplate>
<!--行模板 RowDetailsTemplate-->
<DataTemplate x:Key="RowDetails">
<Border BorderThickness="0" Background="Orchid" Padding="10">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Math:" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="{Binding Math}" VerticalAlignment="Center" FontSize="15" FontWeight="Bold"></TextBlock>

</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="History:" VerticalAlignment="Center" ></TextBlock>
<TextBlock Text="{Binding Histrory}" VerticalAlignment="Center" FontSize=" 15" FontWeight="Bold"></TextBlock>
</StackPanel>
</StackPanel>
</Border>

</DataTemplate>

</Window.Resources>
<Grid Margin="0,0,0,-111">
<Button x:Name="BtnToDataTable" Content="DataGridToDataTable" HorizontalAlignment="Left" Margin="394,690,0,0" VerticalAlignment="Top" Width="240" Height="39" Click="BtnToDataTable_Click"/>
<DataGrid x:Name="dataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="95" Margin="0,17,0,0" VerticalAlignment="Top" Width="634">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Age" Width="50" Binding="{Binding Age}"/>
<DataGridComboBoxColumn Width="80" Header="Sex" SelectedItemBinding="{Binding Sex}"
ItemsSource="{Binding Source={StaticResource sexEnum}}" >
</DataGridComboBoxColumn>
<DataGridCheckBoxColumn Header="Pass Exam?" Width="100" Binding="{Binding Pass}">

</DataGridCheckBoxColumn>

<DataGridHyperlinkColumn Header="Email" Width="150" Binding="{Binding Email}">

</DataGridHyperlinkColumn>
</DataGrid.Columns>

</DataGrid>
<Button x:Name="btnselectMode" Content="选择模式" HorizontalAlignment="Left" Margin="694,10,0,0" VerticalAlignment="Top" Width="75" Height="39" Click="BtnselectMode_Click"/>
<CheckBox x:Name="chkReadOnly" Content="只读" HorizontalAlignment="Left" Margin="725,64,0,0" VerticalAlignment="Top" Click="ChkReadOnly_Click"/>
<DataGrid x:Name="dataGrid1" ItemsSource="{Binding}" AutoGenerateColumns="False"
SelectionUnit="CellOrRowHeader" IsReadOnly="True"
HorizontalAlignment="Left" Height="109" Margin="0,117,0,0" VerticalAlignment="Top" Width="634">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader" >
<Setter Property="Background">

<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="Yellow" Offset="0.5"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="13" />
</Style>
</DataGrid.ColumnHeaderStyle>

<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}">
<DataGridColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="SkyBlue" Offset="0.5"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="Width" Value="80"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ToolTip" Value="Sort by Column"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridColumn.HeaderStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Age" Binding="{Binding Age}">
<DataGridColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="LightGreen" Offset="0.5"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="Width" Value="50"/>
</Style>
</DataGridColumn.HeaderStyle>
</DataGridTextColumn>
<DataGridComboBoxColumn Header="Sex"
SelectedItemBinding="{Binding Sex}"
ItemsSource="{Binding Source={StaticResource sexEnum}}">
<DataGridColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="LightPink" Offset="0.5"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="Width" Value="70"/>
</Style>
</DataGridColumn.HeaderStyle>
</DataGridComboBoxColumn>
<DataGridCheckBoxColumn Header="Pass Exam?" Width="100"
Binding="{Binding Pass}"/>

<DataGridHyperlinkColumn Header="Email" Width="150"
Binding="{Binding Email}"/>
</DataGrid.Columns>
<DataGrid.RowHeaderStyle>
<Style TargetType="DataGridRowHeader">
<Setter Property="Content" Value=" ·¤"/>
<Setter Property="Width" Value="10"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="SkyBlue" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ToolTip" Value="Select this Row"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowHeaderStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="SeaGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
<Label Content="dataGrid" HorizontalAlignment="Left" Margin="650,64,0,0" VerticalAlignment="Top"/>
<Label Content="dataGrid1" HorizontalAlignment="Left" Margin="650,168,0,0" VerticalAlignment="Top"/>
<Label Content="dataGrid2" HorizontalAlignment="Left" Margin="650,297,0,0" VerticalAlignment="Top"/>
<DataGrid x:Name="dataGrid2" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="137" Margin="0,226,0,0" VerticalAlignment="Top" Width="634">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Age" Width="50" Binding="{Binding Age}"/>
<DataGridComboBoxColumn Width="80" Header="Sex" SelectedItemBinding="{Binding Sex}"
ItemsSource="{Binding Source={StaticResource sexEnum}}" >
</DataGridComboBoxColumn>
<DataGridCheckBoxColumn Header="Pass Exam?" Width="100" Binding="{Binding Pass}">

</DataGridCheckBoxColumn>

<DataGridTemplateColumn Header="Exam Date" CellTemplate="{StaticResource DateTemplate }" CellEditingTemplate="{StaticResource EditingDateTemplate }"></DataGridTemplateColumn>

<DataGridHyperlinkColumn Header="Email" Width="150" Binding="{Binding Email}">

</DataGridHyperlinkColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dataGrid3" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="137" Margin="0,365,0,0" VerticalAlignment="Top" Width="634"
AutoGenerateColumns="False" SelectionUnit="FullRow" RowDetailsTemplate="{StaticResource RowDetails}"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Age" Width="50" Binding="{Binding Age}"/>
<DataGridComboBoxColumn Width="80" Header="Sex" SelectedItemBinding="{Binding Sex}"
ItemsSource="{Binding Source={StaticResource sexEnum}}" />
<DataGridCheckBoxColumn Header="Pass Exam?" Width="100" Binding="{Binding Pass}"/>

<DataGridTemplateColumn Header="Exam Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}"/>

<DataGridHyperlinkColumn Header="Email" Width="150" Binding="{Binding Email}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dataGrid4" ItemsSource="{Binding}" Margin="10,511,158,194">

</DataGrid>

<Label Content="dataGrid3" HorizontalAlignment="Left" Margin="650,424,0,0" VerticalAlignment="Top"/>
<Label Content="dataGrid4" HorizontalAlignment="Left" Margin="650,575,0,0" VerticalAlignment="Top"/>

</Grid>
</Window>

后台 C# 源码

using System;

using System.Windows;
using System.Data;
using System.Collections.ObjectModel;

namespace WpfApp1.UI
{
/// <summary>
/// DataGridExample.xaml 的交互逻辑
/// </summary>
public partial class DataGridExample : Window
{
public DataGridExample()
{
InitializeComponent();

ObservableCollection<Member> memberData = new ObservableCollection<Member>();
memberData.Add(new Member()
{
Name = "杨幂",
Age = "28",
Sex = SexOpt.Female,
ExamDate = new DateTime(2010, 4, 10),
Pass = true,
Math=80,
Histrory=100,
Email = new Uri("mailto:[email protected]")
});
memberData.Add(new Member()
{
Name = "阿华",
Age = "40",
Sex = SexOpt.Female,
ExamDate = new DateTime(2010, 4, 10),
Pass = true,
Math = 95,
Histrory = 98,
Email = new Uri("mailto:[email protected]")
});
memberData.Add(new Member()
{
Name = "刘德华",
Age = "55",
ExamDate = new DateTime(2010, 4, 10),
Sex = SexOpt.Male,
Pass = true,
Math = 100,
Histrory = 100,
Email = new Uri("mailto:[email protected]")
});
dataGrid.DataContext = memberData;

dataGrid1.DataContext = memberData;

dataGrid2.DataContext = memberData;

dataGrid3.DataContext = memberData;
}

private void BtnselectMode_Click(object sender, RoutedEventArgs e)
{
dataGrid.SelectionUnit = System.Windows.Controls.DataGridSelectionUnit.Cell;
dataGrid.SelectionMode = System.Windows.Controls.DataGridSelectionMode.Extended;

}

private void ChkReadOnly_Click(object sender, RoutedEventArgs e)
{
if (chkReadOnly.IsChecked == true)
{
dataGrid.IsReadOnly = true;
}
else
{
dataGrid.IsReadOnly = false;
}
}

private void BtnToDataTable_Click(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Columns.Add("Sex", typeof(string));
dt.Columns.Add("Pass",typeof(bool));
dt.Columns.Add("ExamDate", typeof(DateTime));
dt.Columns.Add("Email", typeof(string));
dt.Columns.Add("Math", typeof(int));
dt.Columns.Add("Histrory",typeof(int));

ObservableCollection<Member> OCMember = (ObservableCollection<Member>)dataGrid3.ItemsSource;

foreach(Member m in OCMember)
{
DataRow dr= dt.NewRow();
dr["Name"] = m.Name;
dr["Age"] = m.Age;
dr["Sex"] = m.Sex;
dr["Pass"] = m.Pass;
dr["ExamDate"] = m.ExamDate;
dr["Email"] = m.Email;
dr["Math"] = m.Math;
dr["Histrory"] = m.Histrory;
dt.Rows.Add(dr);
}
// dt = ((DataView)dataGrid3.ItemsSource).ToTable();
dataGrid4.ItemsSource = dt.DefaultView;
}
}

public enum SexOpt { Male,Female};

public class Member
{
public string Name { get; set; }
public string Age { get; set; }

public SexOpt Sex { get; set; }

public bool Pass { get; set; }

public DateTime ExamDate { get; set; }

public Uri Email
{
get; set;
}

public int Math { get; set; }

public int Histrory { get; set; }
}
}

原文地址:https://www.cnblogs.com/zdc-shine/p/11213354.html

时间: 2024-10-19 08:56:09

WPF DataGrid 示例 应用源码的相关文章

经典炫酷的HTML5/jQuery动画应用示例及源码

jQuery是一款普遍受前端开发者欢迎的Javascript框架,但是开发者貌似更关注jQuery开发的插件,海量的jQuery插件让前端开发者非常方便.HTML5的加入让jQuery这个家族更加丰富多彩,因为利用HTML5,我们可以制作更加绚丽动感的动画特效,HTML5结合jQuery,真是我们开发者的福利啊.本文分享了一些经典炫酷的HTML5/jQuery动画应用,喜欢的朋友可以分享和收藏. jquery视差滑块幻灯特效 很传统的一款jQuery焦点图插件,但是该焦点图插件是宽屏的,整体看起

德卡Z90读卡器读取社保卡,德卡Z90读卡器CSharp示例程序源码

前言,最近学习调用 医保卡业务,使用德卡读卡器,主要就是调用一个DLL,动态库文件. 借着自学的机会把心得体会都记录下来,方便感兴趣的小伙伴学习与讨论. 内容均系原创,欢迎大家转载分享,但转载的同时别忘了注明作者和原文链接哦.本文内容和图片如果有侵犯您的权利,请联系[email protected]. 一,准备工作: 先到德卡官网下载相关资料,下载动态库.http://www.decard.com/download/downloadcategoryid=10&isMode=false.html下

9款让你眼前一亮的HTML5/CSS3示例及源码

1.HTML5 3D点阵列波浪翻滚动画 今天我们要再分享一款基于HTML5 3D的点阵列波浪翻滚动画特效,同样是非常的壮观. 在线演示 源码下载 2.HTML5小球弹跳动画 很不错的3D小球 今天我要向大家分享一款很逼真的HTML5动画特效,它是3个色彩各异的弹跳小球,每一个小球在弹跳的时候都会有变化的小球投影,让整个动画更加逼真,而且具有3D的视觉效果. 在线演示 源码下载 3.纯CSS3纸片层叠而成的庙宇动画 今天要分享的这款纯CSS3动画非常特别,动画主体的原型是一座庙宇的屋顶,它用3张纸

图片按日期分类和查看程序(WPF开发)(附源码)

手机方便了我们的生活,可以随时随地拍摄.越来越多的图片堆砌在电脑里.看到杂乱无章的图片,实在感到头痛.手动整理太复杂.基于此,我写了一个小程序,可以将图片按日期整理和查看.按日期查看图片,回忆过去的点点滴滴! 手机拍摄图片时,图片文件会存储拍摄时间,也就是exif信息.通过读取exif信息,可以知晓拍摄时间,程序就是根据该值排序的.查看文件的属性可以看到如下信息: 这个程序有两个功能:按日期整理图片,按日期查看图片. 1 按日期整理图片 功能选项:选择图片存放目录,整理后目录.分类依据:就是分类

16Aspx.com源码2014年7月详细

        Web电子商务网(三层)V2.0源码 2014-07-31   [VS2010] 源码介绍: Web电子商务网(三层)V2.0源码 源码描述: 一.源码特点     采用三层架构开发,购物车功能 二.功能介绍     前台集成了产品在线展示,用户注册.在线调查.在线投稿     后台有类别管理\图书管理\订单管理\会员管理\配送范围管理\邮件列表\广告管理\友情链接管理等     后台添加图书时自动生成缩略图和文字水印     主要参考了petshop的设计架构.使用了Asp.n

自己封装的常用NPOI文件导出源码

示例: 1. 2.示例2 源码下载地址:https://github.com/aa1356889/NPOICode

17+个ASP.NET MVC扩展点,含源码{转}

1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.在自定义的HttpModule中,可以将一个方法注册到HttpApplication的任意一个事件中,在之后执行HttpApplication一些列事件时,按照事件的顺序(事件又按照添加方法先后的顺序)执行注册在事件中的方法! namespace MvcStore.Models { public class Excute

简约之美Jodd-http--深入源码理解http协议

Jodd 是一个开源的 Java 工具集, 包含一些实用的工具类和小型框架.简单,却很强大! jodd-http是一个轻巧的HTTP客户端.现在我们以一个简单的示例从源码层看看是如何实现的? HttpRequest httpRequest = HttpRequest.get("http://jodd.org"); //1. 构建一个get请求 HttpResponse response = httpRequest.send(); //2.发送请求并接受响应信息 System.out.p

出售51aspx上的商业源码 贵的我只要1/3价格 便宜的只要半价 QQ联系 847129860

ahsupermarketshopping AH外贸公司英文企业网站源码 http://www.51aspx.com/Code/AHForeignTradeCompanyrayxietongoa RayOA协同办公服务平台源码 http://www.51aspx.com/code/RayXieTongOAxtright Ext.net权限管理(带CMS)系统源码 http://www.51aspx.com/code/ExtRightCMSlargeenterpriseserp 大型企业通用管理E