C#WPFDataGrid隔⾏变⾊及内容居中对齐
dqzww NET学习
先看效果:
前台XAML代码:
<!--引⼊样式⽂件-->
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary  Source="/Css/DataGridCss.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="contentCenterStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</ResourceDictionary>
</Window.Resources>
<!--XAML中DataGrid-->
<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>
前台XAML的风格代码
<ResourceDictionary xmlns=""
xmlns:x="">
<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>
text align center
<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>
问题:当选中时背景⾊⽆法变化,可去掉DataGridCell的代码。但去掉后⽂本⽆法垂直居和⽔平居中了。
解决办法:
在引⼊样式⽂件中添加⼀个风格定义,DataGridCell的每个⽂本实际是属于TextBlock
<Style x:Key="contentCenterStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
然后再XAML的DataGridTextColumn 加⼊ElementStyle="{StaticResource contentCenterStyle}"
------------------------------------------------------------------------------------------------
⼀般我们要实现居中设置 HorizontalContentAlignment="Center" VerticalContentAlignment="Center"就可以了,但是datagrid的DataGridTextColumn中却发现没有HorizontalContentAlignment或者HorizontalAlignment,列中的内容仍然是左对齐,如何处理才能居中呢?
// 右对齐风格
Style styleRight = new Style(typeof(TextBlock));
Setter setRight = new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right);styleRight.Setters.Add(setRight);
foreach (DataGridColumn c in yourDataGrid.Columns)
{        DataGridTextColumn tc = c as DataGridTextColumn;
if (tc != null)
{
tc.ElementStyle = styleRight;
}
}
即只要设置DataGridColumn的ElementStyle就可以了,也可以在xaml中设置
<Style x:Key="contentCenterStyle"
TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment"
Value="Center" />
</Style>
DataGridTextColumn 中引⽤ElementStyle
<DataGridTextColumn Header="代码"
ElementStyle="{StaticResource contentCenterStyle}"
Binding="{Binding Name}"></DataGridTextColumn>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。