【问题标题】:WPF Data grid Background color of selected rowWPF 数据网格 所选行的背景颜色
【发布时间】:2020-06-15 07:37:36
【问题描述】:

我想设置所选行的背景颜色。请看下面的代码。 前景色(文本颜色)可以工作,但不会改变行的背景色。

<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】:

    现在为行本身添加特定样式后问题已解决。当我添加单元格时它不起作用。

        <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>
    

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 2011-11-04
      • 2015-10-11
      • 2011-02-20
      • 2018-10-14
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多