欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF中设置DataGrid的一些样式

程序员文章站 2022-07-13 22:44:34
...
<Window.Resources>  
    <Style TargetType="DataGrid">  
        <!--拖动改变单元格大小-->  
        <Setter Property="CanUserResizeColumns" Value="false"/>  
        <!--网格线颜色-->  
        <Setter Property="HorizontalGridLinesBrush">  
            <Setter.Value>  
                <SolidColorBrush Color="LightBlue"/>  
            </Setter.Value>  
        </Setter>  
        <Setter Property="VerticalGridLinesBrush">  
            <Setter.Value>  
                <SolidColorBrush Color="LightBlue"/>  
            </Setter.Value>  
        </Setter>  
    </Style>  
  
    <!--标题栏样式-->  
    <Style  TargetType="DataGridColumnHeader" >  
        <Setter Property="Width" Value="50"/>  
        <Setter Property="FontSize" Value="14" />  
        <Setter Property="Background" Value="lightBlue" />  
        <!--加粗-->  
        <Setter  Property="FontWeight"  Value="Bold"/>  
    </Style>  
  
    <!--行样式触发-->  
    <!--背景色改变必须先设置cellStyle 因为cellStyle会覆盖rowStyle样式-->  
    <Style  TargetType="DataGridRow">  
        <Style.Triggers>  
            <Trigger Property="IsSelected" Value="True">  
                <Setter Property="BorderBrush" Value="Red" />  
                <Setter Property="BorderThickness" Value="1" />  
            </Trigger>  
            <Trigger Property="IsMouseOver" Value="True">  
                <Setter Property="Background" Value="LightGray"/>  
            </Trigger>  
        </Style.Triggers>  
    </Style>  
      
    <!--单元格样式触发-->  
    <Style TargetType="DataGridCell">  
        <Style.Triggers>  
            <Trigger Property="IsSelected" Value="True">  
                <Setter Property="Background" Value="White"/>  
                <Setter Property="BorderThickness" Value="0"/>  
                <Setter Property="Foreground" Value="Red"/>  
            </Trigger>  
        </Style.Triggers>  
    </Style>  
</Window.Resources>