所以这是我的数据网格,但是如果我在数据网格中选择一个项目,它的颜色不会改变。触发器不起作用。
DataGrid x:Name="dgAdatok" ColumnWidth="*" MinColumnWidth="100" FontSize="9.5" Background="Transparent" Grid.RowSpan="4" BorderThickness="0" Margin="0,30,0,0" >
<DataGrid.Resources>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="Purple"/>
<Setter Property="Foreground" Value="White"/>
</Style>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="#DA34AE"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderBrush" Value="#DA34AE"/>
</Style>
<Style TargetType="DataGrid">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>
我只需将
<Trigger Property="DataGridCell.IsSelected" Value="True"> <Setter Property="Background" Value="Blue" /> </Trigger>
移动到目标类型 datagridcell
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="#DA34AE"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderBrush" Value="#DA34AE"/>
<!-- Add trigger for IsSelected property -->
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<!-- Change the background color when selected -->
<Setter Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
您正在尝试在
DataGrid
本身上设置触发器,这不是 IsSelected
属性的正确目标。您应该修改 DataGridCell
样式以包含 IsSelected
属性的触发器。