WPF DataGrid 样式设置

隔行换色,鼠标单击,悬浮样式都有,其具体效果如图 1 所示。

图 1 WPF DataGrid 样式设置效果图

其中:

界面设计代码下所示

?

+ 查看代码

1

2

3

4

5

6

7

8

9

10

11

12

<DataGrid AutoGenerateColumns="False"  Name="dataGrid1"  VerticalAlignment="Top"

      CanUserSortColumns="False"  Width="660"   Margin="5" IsReadOnly="True"

      CanUserResizeColumns="False" CanUserResizeRows="False"  SelectionMode="Single"

    CanUserReorderColumns="False" AlternationCount="2"  RowHeaderWidth="0" CanUserAddRows="False" >

        <DataGrid.Columns>

        <DataGridTextColumn Header="名称" Width="150"  Binding="{Binding  Name}"/>

        <DataGridTextColumn Header="最新价"   Width="120"  Binding="{Binding Zxj}"/>

        <DataGridTextColumn Header="涨跌"  Width="120"  Binding="{Binding Zd}"/>

        <DataGridTextColumn Header="涨幅"  Width="130"  Binding="{Binding Zf}"/>

        <DataGridTextColumn Header="短线强势股"  Width="140"  Binding="{Binding Dxqsg}"/>

    </DataGrid.Columns>

</DataGrid>

如果是想把整个所开发的应用程序都更改为这个样式,则在App.xaml文档里面添加相应的样式代码,如果只是当前界面,则在当前界面添加相应的样式代码,样式具体代码如下:

?

+ 查看代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

<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="LightGray"/>

                <!--<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>

转载请注明:学者@李双琳 » 转载:WPF DataGrid 样式分享

时间: 2024-10-13 07:56:21

WPF DataGrid 样式设置的相关文章

wpf 中DataGrid 控件的样式设置及使用

本次要实现的效果为: 这个DataGrid需要绑定一个集合对象,所以要先定义一个Experience类,包含三个字段 /// <summary> /// 定义工作经历类 /// </summary> public class Experience { /// <summary> /// 获取或设置工作的起始时间 /// </summary> public string Start { get; set; } /// <summary> /// 获

WPF DataGrid自定义样式

WPF DataGrid自定义样式 微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. 在DataGrid中的最高水平,你可以改变的外观和感觉,通过设置一些: Property Type Values Default AlternatingRowBackground Brush Any Brush Null Background Brush Any

WPF自定义样式篇-DataGrid

WPF自定义样式篇-DataGrid 先上效果图: 样式: <!--DataGrid样式-->    <Style TargetType="DataGrid">        <Setter Property="RowHeaderWidth" Value="0"></Setter>        <Setter Property="AutoGenerateColumns"

进行手持设备wince开发中DataGrid的样式设置(宽度)

private void SelectGoodsForm_Load(object sender, EventArgs e) { //定义 DataGrid样式 DataGridTableStyle dataGridStyle = null; dataGridStyle = new DataGridTableStyle(); this.goodsGrid.DataSource = createDataTable(); //清楚之前的DataGrid样式 this.goodsGrid.TableSt

WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失效,就是点击他的列表头无法进行排序了.xaml如下:<DataGridTextColumn Width="100" Header="{res:Localize Flexem.Studio.HMIControls.AddressLabel.DataType}">

WPF DataGrid 每行ComboBox 内容不同的设置方法

原文:WPF DataGrid 每行ComboBox 内容不同的设置方法 <toolkit:DataGridComboBoxColumn x:Name="DgCbcSignal" Header="信号源" SelectedItemBinding="{Binding SelectedSignal}" > <toolkit:DataGridComboBoxColumn.ElementStyle> <Style Targe

WPF DataGrid常用属性记录

WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭合DataGrid的行分组. CommitEdit:确认DataGrid的编辑完成. ExpandRowGroup:展开DataGrid的行分组. GetGroupFromItem:从具体Item中得到分组. ScrollIntoView:滚动DataGrid视图. 组件常用属性: Alternat

C# WPF DataGrid 隔行变色及内容居中对齐

C# WPF DataGrid 隔行变色及内容居中对齐. dqzww NET学习0 先看效果: 前台XAML代码: <!--引入样式文件--> <Window.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary  Source="/Css/Data

WPF DataGrid 之数据绑定

1. Auto generation of columns 最简单的方法莫过于让DataGrid根据数据源中的字段自动生成列了: 根据实体类的公共属性, 能够自动生成四种类型的数据列,对应关系如下: TextBox columns for string values; CheckBox columns for boolean values; ComboBox columns for enumerable values; Hyperlink columns for Uri values; 拖个Da