1

在 Excel 中,蓝色背景选择颜色看起来是半透明的,因此即使选择了单元格,用户仍然可以看到真实的背景颜色。使用 WPF 数据网格如何实现相同的效果?

在我的特殊情况下,单元格的Background颜色也绑定到视图模型。我最初的印象是在单元格样式中使用触发器:

  <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="????"></Setter>
      </Trigger>
  </Style.Triggers>

然后以某种方式Value应该计算为...蓝色和未选择Background颜色的某种组合?我尝试使用透明蓝色的画笔,结果与之前的单元格背景颜色不融合。

4

2 回答 2

4

您尝试过的应该确实有效。

看这个例子:

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="10*"/>
    </Grid.RowDefinitions>
    <Rectangle Fill="#FF0090FF"/>
    <DataGrid Grid.Row="1" ItemsSource="{Binding TestCollection}" Background="Purple">
        <DataGrid.Resources>
            <Style TargetType="DataGridRow">
                <Setter Property="Background" Value="Yellow"/>
            </Style>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="#6F0090FF"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
    </DataGrid>
</Grid>

在此处输入图像描述

有蓝色矩形。Fill属性设置#FF0090FF为. 您应该注意的是,您所称"UnSelected Background color"的实际上BackgroundDataGridRow.

Fill现在......所选单元格的背景颜色与该矩形(#0090FF)的背景颜色完全相同。我只更改Alpha为大约 50%( #6F0090FF)。正如您所期望的那样,您看到的是绿色的东西。

在此处输入图像描述

更新

当你想保存Background时,DataGridCell你必须选择不同的方法。我建议在你的DataGridCell's ControlTemplate

这个层......我们可以称之为例如“selectedCellVisual”只有在选择单元格时才可见。

所以...我们必须创建因此我们必须从前面ControlTemplate的示例中更改样式。DataGridCell

<Style TargetType="DataGridCell" >
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="Background" Value="Red"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <Grid>
                                    <Rectangle x:Name="selectedCellVisual" Fill="#6F0090FF" Visibility="Collapsed"/>
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter TargetName="selectedCellVisual" Property="Visibility" Value="Visible"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

现在DataGridCell有红色Background,当它被选中时,半透明的蓝色矩形“selectedCellVisual”是可见的。背景上的透明蓝色和红色使最终的紫罗兰色。

在此处输入图像描述

你应该注意

<Setter Property="OverridesDefaultStyle" Value="True"/>

它必须在那里......因为我们没有更改IsSelected更改单元格背景的属性的触发器。它以默认样式定义。它的变化BackgroundForeground属性。您可以在第一个示例中注意到这一点。前景设置为白色,单元格周围有一像素宽的蓝线。

于 2013-08-07T21:55:09.623 回答
1

基于这个答案,我找到了一个覆盖数据网格的系统颜色的解决方案,例如为系统突出显示颜色添加一些透明度并在突出显示期间使用常规文本颜色:

    <DataGrid>

        <DataGrid.Resources>
            <SolidColorBrush 
                x:Key="{x:Static SystemColors.HighlightBrushKey}"
                Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" 
                Opacity="0.2"/>
            <SolidColorBrush 
                x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
                Color="{DynamicResource {x:Static SystemColors.WindowTextColorKey}}"/>
        </DataGrid.Resources>

    ...
于 2014-10-23T21:25:21.277 回答