Skip to main content
 首页 » 编程设计

c#之仅为选定行更改 HorizontalGridLinesBrush

2024年08月12日67Renyi-Fan

在 WPF“DataGrid”中,水平网格线的颜色可以通过 Horizo​​ntalGridLinesBrush 属性进行修改。

有些项目的 bool 属性设置为 true,所以我想用水平网格线画笔设置为另一种颜色来突出显示它们。

是否可以仅针对某些行更改水平网格线的颜色?

请您参考如下方法:

DataGrid.Horizo​​ntalGridLinesBrush 是根据 DataGrid 设置的,因此您不能按行更改它,但您可以通过禁用水平网格线和创建自定义 来替换默认的水平线行为DataGridRow 样式

<DataGrid ... GridLinesVisibility="Vertical"> 
    <DataGrid.RowStyle> 
        <Style TargetType="{x:Type DataGridRow}"> 
            <Setter Property="BorderThickness" Value="0,0,0,1"/> 
            <Setter Property="BorderBrush" Value="Black"/> 
            <Style.Triggers> 
 
                <!-- this will trigger when row is selected --> 
                <Trigger Property="IsSelected" Value="True">                         
                    <Setter Property="BorderBrush" Value="Red"/> 
                </Trigger> 
 
                <!-- this will trigger when Highlight property of the view model is true --> 
                <DataTrigger Binding="{Binding Highlight}" Value="True"> 
                    <Setter Property="BorderBrush" Value="Green"/> 
                </DataTrigger> 
            </Style.Triggers> 
        </Style> 
    </DataGrid.RowStyle> 
</DataGrid>