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 列表头.行表头.行.单元格相关的 ...
随机推荐
- 14.7.4 InnoDB File-Per-Table Tablespaces
14.7.4 InnoDB File-Per-Table Tablespaces 从历史上看,所有的InnoDB 表和indexes 是存储在system 表空间. 这个整体的方法是针对机器是整个用于 ...
- BZOJ1135: [POI2009]Lyz
1135: [POI2009]Lyz Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 264 Solved: 106[Submit][Status] ...
- c#后台验证
#region 后台验证 panda /// 验证电话号码的主要代码如下: public bool IsTelephone(string str_telephone) { return System. ...
- C/S结构与B/S结构的特点分析
C/S结构与B/S结构的特点分析 为了区别于传统的C/S模式,才特意将其称为B/S模式.认识到这些结构的特征,对于系统的选型而言是很关键的. 1.系统的性能 在系统的性能方面,B/S占有优势的是其异地 ...
- [转]Ubuntu上的包管理:dpkg,apt和aptitude
一直以来对于ubuntu的包管理的概念就是apt-get,偶尔手动装个包就是dpkg -i,现在觉得是要系统地了解一下这几个包管理的命令. 原文转自: http://zhouliang.pro/201 ...
- android.mk android源码编译
http://www.cnblogs.com/chenbin7/archive/2013/01/05/2846863.html Android.mk简单分析 2013-01-05 22:51 by . ...
- (组合数学3.1.1.1)POJ 1146 ID Codes(字典序法)
/* * POJ_1146.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> #i ...
- 什么要缓存curl资源
在看公司的代码框架底层时,发现了一个问题,如下: 代码中调用接口时,使用的是curl,框架将curl资源以IP :端口的形式缓存了下来,例如: 10.10.10.10:80 curl1 10. ...
- HTML5 Security Cheatsheet使用说明
1.URL: https://html5sec.org/ 2.通过点击如图button(也可点击其他:xss firefox)那行的button可以搜索所有button的Cheatsheet,查看都有 ...
- Python中逗号作用的实例分析
逗号在类型转化中的使用 主要是元组的转换 例如: >>> a=11>>> b=(a)>>> b11>>> b=(a,)>& ...