WPF DropShadowEffect打破按钮中的文本

时间:2013-04-17 09:37:56

标签: wpf xaml button user-interface dropshadow

我已经为我的按钮定义了一种风格,它使用了深浅的阴影。 我在网上看到这有时会导致文本模糊,但可以通过使用以下方式在WPF4中解决:

TextOptions.TextFormattingMode="Display"

然而,我的按钮中的文字并不模糊,但它没有正确显示,上面的代码没有改善显示效果。

带有阴影: enter image description here

没有阴影:enter image description here

投影以应用于按钮的样式定义。

<Style TargetType="Button" x:Key="RedButton">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="MinHeight" Value="25" />
    <Setter Property="MinWidth" Value="70" />
    <Setter Property="FontFamily" Value="Verdana" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border TextBlock.Foreground="{TemplateBinding Foreground}" x:Name="Border" CornerRadius="6" BorderThickness="1">
                    <Border.BorderBrush>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <LinearGradientBrush.GradientStops>
                                <GradientStopCollection>
                                    <GradientStop Color="{StaticResource DarkRedColor}" Offset="1.0" />
                                </GradientStopCollection>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="{StaticResource LightRedColor}" Offset="0.5" />
                            <GradientStop Color="{StaticResource DarkRedColor}" Offset="1" />
                        </LinearGradientBrush>
                    </Border.Background>
                    <Border.Effect>
                        <DropShadowEffect Color="Black" Opacity=".50" ShadowDepth="4" RenderingBias="Quality" />
                    </Border.Effect>.....

1 个答案:

答案 0 :(得分:1)

认为这是因为DropShadowEffect试图适用于所有孩子,我想ContentPresenter <{1}}位于Style

试试这个:

<Style x:Key="RedButton"
        TargetType="Button">
  ...
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <Border x:Name="Border"
                  BorderThickness="1"
                  CornerRadius="6"
                  TextBlock.Foreground="{TemplateBinding Foreground}">
            <Border.BorderBrush>
              <LinearGradientBrush StartPoint="0,0"
                                    EndPoint="0,1">
                <LinearGradientBrush.GradientStops>
                  <GradientStopCollection>
                    <GradientStop Offset="1.0"
                                  Color="{StaticResource DarkRedColor}" />
                  </GradientStopCollection>
                </LinearGradientBrush.GradientStops>
              </LinearGradientBrush>
            </Border.BorderBrush>
            <Border.Background>
              <LinearGradientBrush StartPoint="0.5,0"
                                    EndPoint="0.5,1">
                <GradientStop Offset="0.5"
                              Color="{StaticResource LightRedColor}" />
                <GradientStop Offset="1"
                              Color="{StaticResource DarkRedColor}" />
              </LinearGradientBrush>
            </Border.Background>
            <Border.Effect>
              <DropShadowEffect Opacity=".50"
                                RenderingBias="Quality"
                                ShadowDepth="4"
                                Color="Black" />
            </Border.Effect>
          </Border>
          <Border BorderThickness="1"
                  CornerRadius="6"
                  TextBlock.Foreground="{TemplateBinding Foreground}">
            <ContentPresenter />
          </Border>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>