【WPF】通过修改dataGrid的cell的style,改变选中行失去焦点时的颜色

复制代码
<Style  TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger  Property="IsSelected" Value="true">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                </Trigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />
                        <Condition Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
复制代码

 以下是仅修改选中行颜色(之前一直修改失败,原因是修改了RowStyle,这个会被CellStyle替换,故需要直接修改CellStyle

复制代码
<DataGrid.CellStyle>  
    <Style TargetType="DataGridCell">  
        <Setter Property="BorderThickness" Value="0"/>  
        <Setter Property="MinWidth" Value="85"/>     
        <Style.Triggers>  
            <Trigger Property="IsSelected" Value="True">  
                <Setter Property="Background" Value="LightBlue"/>  
                <Setter Property="Foreground" Value="White"/>  
             </Trigger>  
         </Style.Triggers>  
     </Style>  
</DataGrid.CellStyle>  
复制代码

 

posted on   梦琪小生  阅读(868)  评论(0编辑  收藏  举报

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

点击右上角即可分享
微信分享提示