如何在Textblock上添加非模糊的阴影

时间:2013-12-20 18:48:44

标签: c# .net wpf xaml dropshadow

这个问题说明了一切。我无法弄清楚这一点。我试图添加一个镜像文本元素,但我有一个触发器,当鼠标悬停在元素上时会更改fontsize,由于主元素位于顶部,因此不会触发该触发器。模糊的常用解决方法不适用于文本本身要隐藏的内容。

我正在考虑攻击这个并添加两个切换可见性的阴影文本块。但是,我不知道如何切换这种可见性,因为我不能使用TargetNameDataTrigger,因为它基于另一个元素,因此它永远不会触发。

每个请求(阴影被夸大以便可以看到)enter image description here

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:models="clr-namespace:DesktopDictation.Spelling.Models">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../FontStyles/TextBlock.DefaultFont.xaml"/>
  </ResourceDictionary.MergedDictionaries>
  <DropShadowEffect x:Key="BlackShadow" ShadowDepth="10" Direction="270" Color="Black" Opacity="75" BlurRadius="2"/>
  <Style x:Key="Spelling.TextGlyph" TargetType="TextBlock" BasedOn="{StaticResource TextBlock.DefaultFontFamilyStyle}">
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="Foreground" Value="#FFFFFF"/>
    <!--<Setter Property="Effect" Value="{StaticResource BlackShadow}"/>-->
    <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Foreground" Value="#75BAFF"/>
        <Setter Property="FontWeight" Value="SemiBold"/>
        <Setter Property="FontSize" Value="22"/>
      </Trigger>
    </Style.Triggers>
  </Style>
  <Style x:Key="GlyphList" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
      <Setter.Value>
        <DataTemplate DataType="models:SpellingGlyph">
          <Grid Name="MainGrid">
            <TextBlock Text="{Binding Text}" VerticalAlignment="Bottom" Effect="{StaticResource BlackShadow}"  Margin="0,0,5,0"/>
            <TextBlock Text="{Binding Text}" ToolTip="{Binding Pronunciations}" Style="{StaticResource Spelling.TextGlyph}" VerticalAlignment="Bottom" Margin="0,0,5,0"/>             
          </Grid>
        </DataTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

1 个答案:

答案 0 :(得分:0)

我明白了!我必须使用Label才能使用ControlTemplate,然后我可以使用DataTriggerTargetName

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:models="clr-namespace:DesktopDictation.Spelling.Models">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../FontStyles/TextBlock.DefaultFont.xaml"/>
  </ResourceDictionary.MergedDictionaries>
  <DropShadowEffect x:Key="BlackShadow" ShadowDepth="2" Direction="270" Color="Black" Opacity="75" BlurRadius="2"/>
  <Style x:Key="Spelling.TextGlyph" TargetType="TextBlock" BasedOn="{StaticResource TextBlock.DefaultFontFamilyStyle}">
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="Foreground" Value="#FFFFFF"/>
  </Style>
  <Style x:Key="GlyphList" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
      <Setter.Value>
        <DataTemplate DataType="models:SpellingGlyph">
          <Label x:Name="MainSmall" VerticalAlignment="Bottom">
            <Label.Template>
              <ControlTemplate>
                <Grid>
                  <TextBlock Name="Shadow" Text="{Binding Text}" VerticalAlignment="Bottom" Style="{StaticResource Spelling.TextGlyph}" Effect="{StaticResource BlackShadow}" Margin="0,0,5,0"/>
                  <TextBlock Name="MainText" Text="{Binding Text}" ToolTip="{Binding Pronunciations}" Style="{StaticResource Spelling.TextGlyph}" VerticalAlignment="Bottom" Margin="0,0,5,0"/>
                </Grid>
                <ControlTemplate.Triggers>
                  <DataTrigger Binding="{Binding ElementName=MainText, Path=IsMouseOver}" Value="True">
                    <Setter TargetName="MainText" Property="Foreground" Value="#75BAFF"/>
                    <Setter TargetName="MainText" Property="FontWeight" Value="SemiBold"/>
                    <Setter TargetName="MainText" Property="FontSize" Value="22"/>
                    <Setter TargetName="Shadow" Property="Foreground" Value="#75BAFF"/>
                    <Setter TargetName="Shadow" Property="FontWeight" Value="SemiBold"/>
                    <Setter TargetName="Shadow" Property="FontSize" Value="22"/>
                  </DataTrigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Label.Template>
          </Label>
        </DataTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>