DataGrid RowStyle 不适用于 IsSelected 和 IsMouseOver 组合

DataGrid RowStyle not working for IsSelected and IsMouseOver combination

我有一个简单的 DatGrid。我想根据特定条件相应地设置我的行的背景颜色。在此示例中,该行的开头只是红色。

我想将该基本背景色更改为不同的颜色

问题是它适用于单元格但不适用于整行。

我为最后一个要求设置了 MultiTrigger,它改变了单元格的颜色,但没有改变整行的颜色。当我将 CellStyle MultiTrigger 移动到 RowStyle 时,它不再执行任何操作。

<Window x:Class="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfTest" >
    <Window.DataContext>
        <local:MyWindowViewModel />
    </Window.DataContext>
    <Grid>
        <DataGrid ItemsSource="{Binding Items}"
                  AutoGenerateColumns="False" 
                  SelectionMode="Single" SelectionUnit="FullRow" >    
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" Value="Red"/>

                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Green" />
                        </Trigger>                                                  
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>

            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Orange" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="True" />
                                <Condition Property="IsMouseOver" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" Value="Yellow"/>
                        </MultiTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>

            <DataGrid.Columns>
                <DataGridTextColumn Header="FirstName"  Width="50*" Binding="{Binding FirstName}" />
                <DataGridTextColumn Header="LastName"  Width="50*" Binding="{Binding LastName}" />
            </DataGrid.Columns>            
        </DataGrid>
    </Grid>
</Window>

模型/视图模型:

class Model
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class MyWindowViewModel
{
    public List<Model> Items { get; private set; }

    public MyWindowViewModel()
    {
        Items = new List<Model>();
        Items.Add(new Model { FirstName = "AB", LastName = "CD"});
        Items.Add(new Model { FirstName = "EF", LastName = "GH" });
        Items.Add(new Model { FirstName = "IJ", LastName = "KL" });
    }
}

您的 DataGridCell 样式需要一个 DataTrigger,它将绑定到父行 IsSelected,这样所有单元格都会更改样式,而不仅仅是您悬停的单元格。

<DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
    <Setter Property="Background" Value="Yellow"/>
</DataTrigger>