wpf 中DataGrid 控件的样式设置及使用
本次要实现的效果为:

这个DataGrid需要绑定一个集合对象,所以要先定义一个Experience类,包含三个字段
/// <summary>
/// 定义工作经历类
/// </summary>
public class Experience
{
/// <summary>
/// 获取或设置工作的起始时间
/// </summary>
public string Start { get; set; }
/// <summary>
/// 获取或设置工作的结束时间
/// </summary>
public string End { get; set; }
/// <summary>
/// 获取或设置工作地点
/// </summary>
public string Location { get; set; }
}
接下来在 Window_Loaded(object sender, RoutedEventArgs e) 时间中设置DataGrid的数据源
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<Experience> list = new List<Experience>()
{
new Experience(){ Start="1990-01-01", End="1991-01-01", Location="北京"},
new Experience(){ Start="1991-01-01", End="1992-01-01", Location="郑州"},
new Experience(){ Start="1992-01-01", End="1993-01-01", Location="上海"},
new Experience(){ Start="1993-01-01", End="1994-01-01", Location="洛阳"},
new Experience(){ Start="1994-01-01", End="1995-01-01", Location="天津"},
new Experience(){ Start="1995-01-01", End="1996-01-01", Location="大连"},
};
//把XAML中DataGrid的ItemSource绑定到List对象上去
this.grid_user.ItemsSource = list;
}
接下来看一下XAML中是怎样创建DataGrid
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition />
</Grid.RowDefinitions>
<Label Background="#82c772"
Content="个人经历"
Padding="20,5,0,0"
FontSize="14"
Foreground="White" />
<DataGrid Name="grid_saffer"
Grid.Row="1"
IsReadOnly="True"
AlternationCount="2"
AutoGenerateColumns="true" >
<DataGrid.Columns>
<DataGridTextColumn Header="开始时间"
Width="1*"
Binding="{Binding start}" />
<DataGridTextColumn Header="结束时间"
Width="1*"
Binding="{Binding end}" />
<DataGridTextColumn Header="地点"
Width="1*"
Binding="{Binding location}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
这里有几个属性需要说一下
AlternationCount="2" 表示两行交替显示背景色。如果不设置此属性则显示效果为:

AutoGenerateColumns="False" 表示不让DataGrid自动生成列。如果设置成true,则效果如下: 多出了不需要的列

HeadersVisibility="Column" 设置此属性为只显示列标题单元格,行标题不显示。如果不设置此属性则效果为:

到此为止还没有真正为DataGrid添加任何样式,接下来展示样式代码
<Application x:Class="ListviewStyle.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:location="clr-namespace:ListviewStyle" StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="DataGrid">
<!--网格线颜色-->
<Setter Property="CanUserResizeColumns" Value="false" /> //该属性指示是否允许用户调整列宽度
<Setter Property="Background" Value="#E6DBBB" />
<Setter Property="BorderBrush" Value="#d6c79b" />
<Setter Property="HorizontalGridLinesBrush">
<Setter.Value>
<SolidColorBrush Color="#d6c79b" />
</Setter.Value>
</Setter>
<Setter Property="VerticalGridLinesBrush">
<Setter.Value>
<SolidColorBrush Color="#d6c79b" />
</Setter.Value>
</Setter>
</Style>
<!--标题栏样式-->
<!--<Style TargetType="DataGridColumnHeader" >
<Setter Property="Width" Value="50"/>
<Setter Property="Height" Value="30"/>
<Setter Property="FontSize" Value="14" />
<Setter Property="Background" Value="White" />
<Setter Property="FontWeight" Value="Bold"/>
</Style>-->
<Style TargetType="DataGridColumnHeader">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="MinWidth" Value="0" />
<Setter Property="MinHeight" Value="28" />
<Setter Property="Foreground" Value="#323433" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridColumnHeader">
<Border x:Name="BackgroundBorder" BorderThickness="0,1,0,1" BorderBrush="#e6dbba" Width="Auto">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ContentPresenter Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Path x:Name="SortArrow" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" Grid.Column="2"
Width="8"
Height="6"
Fill="White"
Margin="0,0,50,0"
VerticalAlignment="Center"
RenderTransformOrigin="1,1" />
<Rectangle Width="1"
Fill="#d6c79b"
HorizontalAlignment="Right"
Grid.ColumnSpan="1" />
<!--<TextBlock Background="Red">
<ContentPresenter></ContentPresenter></TextBlock>-->
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Height"
Value="25" />
</Style>
<!--行样式触发-->
<!--背景色改变必须先设置cellStyle 因为cellStyle会覆盖rowStyle样式-->
<Style TargetType="DataGridRow">
<Setter Property="Background"
Value="#F2F2F2" />
<Setter Property="Height"
Value="25" />
<Setter Property="Foreground"
Value="Black" />
<Style.Triggers>
<!--隔行换色-->
<Trigger Property="AlternationIndex"
Value="0">
<Setter Property="Background"
Value="#e7e7e7" />
</Trigger>
<Trigger Property="AlternationIndex"
Value="1">
<Setter Property="Background"
Value="#f2f2f2" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="#fbe178" />
<!--<Setter Property="Foreground" Value="White"/>-->
</Trigger>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Foreground"
Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
<!--单元格样式触发-->
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<TextBlock TextAlignment="Center"
VerticalAlignment="Center">
<ContentPresenter />
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<!--<Setter Property="Background" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>-->
<Setter Property="Foreground"
Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
建议:样式代码比较多,最好是先复制到自己项目中看效果,然后再细看样式的实现。 在此,内容已介绍完,如有疑问请留言。
wpf 中DataGrid 控件的样式设置及使用的更多相关文章
- winform中DataGrid控件的宽度设置
最近修改一个win5.0的PDA程式,碰到一个问题.就是给DataGrid控件绑定数据的时候,这个控件的宽度不能调整,有时候数据较长,就显示不全.然后想在程式里自定义它的宽度,设置不成功.然后网上没找 ...
- Working Experience - WPF 中 DataGrid 控件的应用
问题: 添加控件后, 编辑单元格会出现异常 绑定 ItemsSource 属性后, 更新绑定对象的数据, UI 不刷新 如何显示控件中 ComboBox 类型 解决方法: 绑定 ItemsSource ...
- EasyUI中datagrid控件的使用 设置多行表头(两行或多行)
EasyUI中的datagrid控件十分强大,能生成各种复杂的报表,现在因为项目需要,需要生成一个表头两行的表,找了一些说明文档,以下用一个实例来说明一下: 第一种方法: $('#divData'). ...
- WPF中DataGrid控件内Button的Command和CommandParameter的绑定
场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...
- WPF中DataGrid控件的过滤(Filter)性能分析及优化
DataGrid控件是一个列表控件, 可以进行过滤,排序等.本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案. 1)DataGrid的过滤过程: 用户输入过滤条件 ...
- WPF中Datagrid控件添加行号
/// <summary> /// 导入Excel文件按钮 /// </summary> /// <param name="sender">&l ...
- WPF中获取控件默认样式和模板XML
从微软官方找这个东西甚是困难,似乎根本没有提供.网上说因为版本问题,很难找到,但通过代码却可以轻易获得.经测试,生成的样式文件非常完美,完全不用修改即可应用. 代码如下: public static ...
- WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇) 在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...
- WPF 4 DataGrid 控件(进阶篇一)
原文:WPF 4 DataGrid 控件(进阶篇一) 上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...
随机推荐
- http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html
http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html T ...
- BZOJ3687: 简单题
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3687 小呆开始研究集合论了,他提出了关于一个数集四个问题: 1.子集的异或和的算术和. 2.子 ...
- 利用python分析nginx日志
最近在学习python,写了个脚本分析nginx日志,练练手.写得比较粗糙,但基本功能可以实现. 脚本功能:查找出当天访问次数前十位的IP,并获取该IP来源,并将分析结果发送邮件到指定邮箱. 实现前两 ...
- HDU-2509 Be the Winner
http://acm.hdu.edu.cn/showproblem.php?pid=2509 Be the Winner Time Limit: 2000/1000 MS (Java/Others) ...
- 《Linear Algebra and Its Applications》-chaper4-向量空间-子空间、零空间、列空间
在线性代数中一个非常重要的概念就是向量空间R^n,这一章节将主要讨论向量空间的一系列性质. 一个向量空间是一些向量元素构成的非空集合V,需要满足如下公理: 向量空间V的子空间H需要满足如下三个条件: ...
- 爬虫技术实战 | WooYun知识库
爬虫技术实战 | WooYun知识库 爬虫技术实战 大数据分析与机器学习领域Python兵器谱-大数据邦-微头条(wtoutiao.com) 大数据分析与机器学习领域Python兵器谱
- request.setAttribute()用法
小问题: JSP1代码 String [] test=new String[2]; test[0]="1"; test[1]="2"; request.setA ...
- 关于fork有意思的两道题目
http://www.spongeliu.com/123.html 第一题,计算下面代码理论上总共打印了多少行:(网易2011笔试题) #include #include #include int m ...
- java 深clone和浅clone
1. clone类 public class Person implements Cloneable, Serializable{ /** * */ private static final long ...
- HDU 4901(杭电多校训练#3 1005题)The Romantic Hero(DP)
题目地址:HDU 4901 这题没想到最后竟然可以做出来.. .. 这题用了两次DP,先从前往后求一次异或的.再从后往前求一次与运算的. 各自是 1:求异或的时候,定义二维数组huo[1000][10 ...