WPF DataGrid 样式设置
2020-12-13 14:20
标签:style http io color os ar for sp strong 隔行换色,鼠标单击,悬浮样式都有,其具体效果如图 1 所示。 图 1 WPF DataGrid 样式设置效果图 其中: 界面设计代码下所示
WPF DataGrid 样式设置 标签:style http io color os ar for sp strong 原文地址:http://www.cnblogs.com/qq247039968/p/4062332.html
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文档里面添加相应的样式代码,如果只是当前界面,则在当前界面添加相应的样式代码,样式具体代码如下:
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
=
"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"
/>
Grid
>
Border
>
ControlTemplate
>
Setter.Value
>
Setter
>
Setter
Property
=
"Height"
Value
=
"25"
/>
Style
>
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"
/>
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
=
"Foreground"
Value
=
"Black"
/>
Trigger
>
Style.Triggers
>
Style
>
ResourceDictionary
>
转载请注明:学者@李双琳 » 转载:WPF DataGrid 样式分享
上一篇:莫队算法(可修改)
下一篇:用c#解析JSON总结