使用文本阴影样式选择DataGrid单元格?

时间:2012-03-15 17:28:54

标签: c# wpf xaml datagrid

我正在尝试在选择行时向单元格文本添加文本阴影。

更新

这就是我现在所拥有的:

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
    <Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ContentPresenter x:Name="CellContent" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="CellContent" Property="TextOptions.TextFormattingMode" Value="Display" />
                        <Setter TargetName="CellContent" Property="RenderOptions.ClearTypeHint" Value="Enabled" />
                        <Setter TargetName="CellContent" Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect ShadowDepth="2" BlurRadius="2" Color="Black" RenderingBias="Quality" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

它只为文本添加阴影(正是我想要的)和选择行时(正如我想要的那样)。现在唯一的问题是我正在失去亚像素抗锯齿:

enter image description here

所选行是下面带蓝色背景的行。它现在使用某种灰色抗锯齿,使它看起来很糟糕。我试图设置RenderOptions.ClearTypeHint但没有成功。如何启用它以使其看起来像没有选择的行一样清晰明了?

这就是灰色抗锯齿在100%大小(模糊)中的样子:

enter image description here

1 个答案:

答案 0 :(得分:4)

细胞的背景阻止您看到效果。只需设置这样的背景,但我不知道你是否欣赏结果:

<Style TargetType="{x:Type DataGridCell}">
  <Setter Property="Background" Value="Transparent"/>
  <Style.Triggers>
    <Trigger Property="IsSelected" Value="True">
      <Setter Property="Effect">
        <Setter.Value>
          <DropShadowEffect ShadowDepth="2"
                            BlurRadius="2"
                            Color="Black"
                            RenderingBias="Quality"/>
        </Setter.Value>
      </Setter>
    </Trigger>
  </Style.Triggers>
</Style>

修改

也许这样更好,因为它可以让你获得更多控制权:

<Style TargetType="{x:Type DataGridCell}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type DataGridCell}">
        <ContentPresenter x:Name="CellContent"/>
        <ControlTemplate.Triggers>
          <Trigger Property="IsSelected" Value="True">
            <Setter TargetName="CellContent" Property="Effect">
              <Setter.Value>
                <DropShadowEffect ShadowDepth="2"
                                  BlurRadius="2"
                                  Color="Black"
                                  RenderingBias="Quality"/>
              </Setter.Value>
            </Setter>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>