WPF Datagrid.RowStyle渐变背景

本文关键字:背景 渐变 RowStyle Datagrid WPF | 更新日期: 2023-09-27 18:09:08

我想将数据网格的行背景从纯色改为渐变。什么好主意吗?

当前代码为:

<DataGrid.RowStyle>
 <Style TargetType="DataGridRow">
  <Style.Triggers>
   <DataTrigger Binding="{Binding GSRowBackground}" Value="0">
    <Setter Property="Background" Value="White"></Setter>
   </DataTrigger>
  </Style.Triggers>
 </Style>
</DataGrid.RowStyle>

解决方案(感谢您为我指出正确的方向):

<DataGrid.RowStyle>
 <Style TargetType="DataGridRow">
  <Style.Triggers>
   <DataTrigger Binding="{Binding GSRowBackground}" Value="0">
    <Setter Property="Background">
     <Setter.Value>
      <LinearGradientBrush StartPoint="1,0" EndPoint="0,1">
       <GradientStop Offset="0" Color="#888888"/>
        <GradientStop Offset="1" Color="#FFFFF86E"/>
      </LinearGradientBrush>
     </Setter.Value>
    </Setter>
   </DataTrigger>
  </Style.Triggers>
 </Style>
</DataGrid.RowStyle>

WPF Datagrid.RowStyle渐变背景

    <LinearGradientBrush x:Key="gradientBrush" StartPoint="0,0" EndPoint="0,1">
       <GradientStop Offset="0" Color="#888888"/>
       <GradientStop Offset="1" Color="#FFFFF86E"/>
    </LinearGradientBrush>
<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridRow}">
                <Border x:Name="DGR_Border" SnapsToDevicePixels="True" Background="{StaticResource gradientBrush}" Padding="0,0,3,3">        
                    <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

解决方案(感谢您为我指出正确的方向):

<DataGrid.RowStyle>
 <Style TargetType="DataGridRow">
  <Style.Triggers>
   <DataTrigger Binding="{Binding GSRowBackground}" Value="0">
    <Setter Property="Background">
     <Setter.Value>
      <LinearGradientBrush StartPoint="1,0" EndPoint="0,1">
       <GradientStop Offset="0" Color="#888888"/>
        <GradientStop Offset="1" Color="#FFFFF86E"/>
      </LinearGradientBrush>
     </Setter.Value>
    </Setter>
   </DataTrigger>
  </Style.Triggers>
 </Style>
</DataGrid.RowStyle>