本次要实现的效果为:

这个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 控件的样式设置及使用的更多相关文章

  1. winform中DataGrid控件的宽度设置

    最近修改一个win5.0的PDA程式,碰到一个问题.就是给DataGrid控件绑定数据的时候,这个控件的宽度不能调整,有时候数据较长,就显示不全.然后想在程式里自定义它的宽度,设置不成功.然后网上没找 ...

  2. Working Experience - WPF 中 DataGrid 控件的应用

    问题: 添加控件后, 编辑单元格会出现异常 绑定 ItemsSource 属性后, 更新绑定对象的数据, UI 不刷新 如何显示控件中 ComboBox 类型 解决方法: 绑定 ItemsSource ...

  3. EasyUI中datagrid控件的使用 设置多行表头(两行或多行)

    EasyUI中的datagrid控件十分强大,能生成各种复杂的报表,现在因为项目需要,需要生成一个表头两行的表,找了一些说明文档,以下用一个实例来说明一下: 第一种方法: $('#divData'). ...

  4. WPF中DataGrid控件内Button的Command和CommandParameter的绑定

    场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...

  5. WPF中DataGrid控件的过滤(Filter)性能分析及优化

    DataGrid控件是一个列表控件, 可以进行过滤,排序等.本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案. 1)DataGrid的过滤过程:      用户输入过滤条件       ...

  6. WPF中Datagrid控件添加行号

    /// <summary> /// 导入Excel文件按钮 /// </summary> /// <param name="sender">&l ...

  7. WPF中获取控件默认样式和模板XML

    从微软官方找这个东西甚是困难,似乎根本没有提供.网上说因为版本问题,很难找到,但通过代码却可以轻易获得.经测试,生成的样式文件非常完美,完全不用修改即可应用. 代码如下: public static ...

  8. WPF 4 DataGrid 控件(自定义样式篇)

    原文:WPF 4 DataGrid 控件(自定义样式篇)      在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...

  9. WPF 4 DataGrid 控件(进阶篇一)

    原文:WPF 4 DataGrid 控件(进阶篇一)      上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...

随机推荐

  1. Duplex Services (Msdn)

    Duplex Services from msdn A duplex service contract is a message exchange pattern in which both endp ...

  2. IIS的ISAPI接口简介

      ISAPI(Internet Server Application Programming Interface)作为一种可用来替代CGI的方法,是由微软和Process软件公司联合提出的Web服务 ...

  3. HDOJ/HDU 2550 百步穿杨(注意排序)

    Problem Description 时维九月,序属三秋,辽军大举进攻MCA山,战场上两军正交锋.辽军统帅是名噪一时的耶律-James,而MCA方则是派出了传统武将中草药123.双方经过协商,约定在 ...

  4. UIImageVIew的使用

    UIImageView是一个用于显示图片的控件 构造方法:     UIImage * tempImage = [UIImage imageNamed:IMAGE_NAME];     imageVi ...

  5. StoryBoard 的使用

    简单入门: http://my.oschina.net/plumsoft/blog/53886 详细操作:http://www.cnblogs.com/buro79xxd/archive/2012/0 ...

  6. 前端开发者应当了解的 Web 缓存知识

    缓存优点 通常所说的Web缓存指的是可以自动保存常见http请求副本的http设备.对于前端开发者来说,浏览器充当了重要角色.除此外常见的还有各种各样的代理服务器也可以做缓存.当Web请求到达缓存时, ...

  7. Linux下开启MySQL的远程连接

    今天在用客户端工具远程连接mysql的时候,连接不上,以为是防火墙,关了防火墙后依然打不开,后开在网上查了下原来mysql基于安全考虑root账户一般只能本地访问,但是在开发过程中可能需要打开root ...

  8. poj 1328贪心

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  9. 深度学习Matlab DeepLearningToolBox 工具包最常见错误解决办法\

    deeplearningtoolbox  下载链接github : https://github.com/rasmusbergpalm/DeepLearnToolbox,只需要解压到matlab当前工 ...

  10. mysql中group_concat函数用法

    该函数返回带有来自一个组的连接的非NULL值的字符串结果.该函数是一个增强的Sybase SQL Anywhere支持的基本LIST()函数. 语法结构: GROUP_CONCAT([DISTINCT ...