一、WPF DataGrid样式(隔行换色、鼠标单击、悬浮样式)
转自:http://www.cnblogs.com/xiaogangqq123/archive/2012/05/07/2487166.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><DataGridTextColumnHeader="名称" Width="150" Binding="{Binding Name}"/><DataGridTextColumnHeader="最新价" Width="120" Binding="{Binding Zxj}"/><DataGridTextColumnHeader="涨跌" Width="120" Binding="{Binding Zd}"/><DataGridTextColumnHeader="涨幅" Width="130" Binding="{Binding Zf}"/><DataGridTextColumnHeader="短线强势股" Width="140" Binding="{Binding Dxqsg}"/></DataGrid.Columns></DataGrid>
style代码:
<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><StyleTargetType="DataGrid"><!--网格线颜色--><SetterProperty="CanUserResizeColumns" Value="false"/><SetterProperty="Background" Value="#E6DBBB"/><SetterProperty="BorderBrush" Value="#d6c79b"/><SetterProperty="HorizontalGridLinesBrush"><Setter.Value><SolidColorBrushColor="#d6c79b"/></Setter.Value></Setter><SetterProperty="VerticalGridLinesBrush"><Setter.Value><SolidColorBrushColor="#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>--><StyleTargetType="DataGridColumnHeader"><SetterProperty="SnapsToDevicePixels" Value="True"/><SetterProperty="MinWidth" Value="0"/><SetterProperty="MinHeight" Value="28"/><SetterProperty="Foreground" Value="#323433"/><SetterProperty="FontSize" Value="14"/><SetterProperty="Cursor" Value="Hand"/><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="DataGridColumnHeader"><Borderx:Name="BackgroundBorder" BorderThickness="0,1,0,1" BorderBrush="#e6dbba" Width="Auto"><Grid><Grid.ColumnDefinitions><ColumnDefinitionWidth="*"/></Grid.ColumnDefinitions><ContentPresenterMargin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"/><Pathx: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"/><RectangleWidth="1" Fill="#d6c79b" HorizontalAlignment="Right" Grid.ColumnSpan="1"/><!--<TextBlock Background="Red"> <ContentPresenter></ContentPresenter></TextBlock>--></Grid></Border></ControlTemplate></Setter.Value></Setter><SetterProperty="Height" Value="25"/></Style><!--行样式触发--><!--背景色改变必须先设置cellStyle 因为cellStyle会覆盖rowStyle样式--><StyleTargetType="DataGridRow"><SetterProperty="Background" Value="#F2F2F2"/><SetterProperty="Height" Value="25"/><SetterProperty="Foreground" Value="Black"/><Style.Triggers><!--隔行换色--><TriggerProperty="AlternationIndex" Value="0"><SetterProperty="Background" Value="#e7e7e7"/></Trigger><TriggerProperty="AlternationIndex" Value="1"><SetterProperty="Background" Value="#f2f2f2"/></Trigger><TriggerProperty="IsMouseOver" Value="True"><SetterProperty="Background" Value="LightGray"/><!--<Setter Property="Foreground" Value="White"/>--></Trigger><TriggerProperty="IsSelected" Value="True"><SetterProperty="Foreground" Value="Black"/></Trigger></Style.Triggers></Style><!--单元格样式触发--><StyleTargetType="DataGridCell"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="DataGridCell"><TextBlockTextAlignment="Center" VerticalAlignment="Center"><ContentPresenter/></TextBlock></ControlTemplate></Setter.Value></Setter><Style.Triggers><TriggerProperty="IsSelected" Value="True"><!--<Setter Property="Background" Value="White"/> <Setter Property="BorderThickness" Value="0"/>--><SetterProperty="Foreground" Value="Black"/></Trigger></Style.Triggers></Style></ResourceDictionary>