WPF数据网格所选行的背景色

问题描述 投票:0回答:1

我想设置所选行的背景色。请看下面的代码。前景色(文字颜色)工作了,但它没有改变行的背景色。

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid>
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="DataGridCell.IsSelected" Value="True">
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="Background" Value="Green" />
        </Trigger>
    </Style.Triggers>
</Style>


<DataGrid x:Name="Details" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,10,10,10"
          ItemsSource="{Binding Details}" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Index}" Header="Index" />
        <DataGridTextColumn Binding="{Binding One}" Header="1" />
        <DataGridTextColumn Binding="{Binding two}" Header="2" />
        <DataGridTextColumn Binding="{Binding three}" Header="3" />
    </DataGrid.Columns>
</DataGrid>
wpf xaml datagrid
1个回答
0
投票

现在,为行本身添加特定的样式后,问题就解决了。当我为单元格添加样式时,问题就解决了。

    <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <Trigger Property="DataGridRow.IsSelected" Value="True">
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="Background" Value="Green" />
        </Trigger>
    </Style.Triggers>
</Style>
© www.soinside.com 2019 - 2024. All rights reserved.