添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在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 表示两行交替显示背景色
  • AutoGenerateColumns 表示不让DataGrid自动生成列
  • HeadersVisibility 设置此属性为只显示行/列/不显示标题单元格
  • CanUserDeleteRows 是否允许删除行
  • CanUserAddRows 是否允许添加行
  • CanUserSortColumns 是否允许列排序
  • CanUserResizeColumns 是否允许改变列宽
  • CanUserResizeRows 是否允许改变行高
  • RowHeaderWidth 行标题宽度
  • ScrollViewer.HorizontalScrollBarVisibility 水平滚动条显示状态
  • ScrollViewer.VerticalScrollBarVisibility 垂直滚动条显示状态


添加样式

<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>
<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="Height" Value="25" />
<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>
</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>