Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a drop shadow style in XAML

I’m struggling a little bit with some XAML syntax I hope someone can advise on. I want to create an “Effect” type style resource which contains a DropShadowEffect definition which can be reused rather than always manually setting the properties. Here’s what I have:

<Style TargetType="DropShadowEffect" x:Name="DropShadowEffectStyle">
  <Setter Property="BlurRadius" Value="5" />
  <Setter Property="Direction" Value="315" />
  <Setter Property="ShadowDepth" Value="2" />
  <Setter Property="Opacity" Value="0.5" />
</Style>

<Style TargetType="TextBlock" x:Name="PageTabLabelStyle">
  <Setter Property="FontSize" Value="16" />
  <Setter Property="FontFamily" Value="Arial" />
  <Setter Property="Foreground" Value="#EFEFEF" />
  <Setter Property="VerticalAlignment" Value="Center" />
  <Setter Property="Margin" Value="0, 10, 0, 10" />
  <Setter Property="Effect" Value="{StaticResource DropShadowEffectStyle}" />
</Style>

This fails dismally each time it runs so I’m obviously missing something. I think it’s around the “Effect” property of the text block style expecting an “Effect” type rather than a “DopShadowEffect” type. Any ideas?

like image 453
Troy Hunt Avatar asked Aug 14 '09 03:08

Troy Hunt


1 Answers

You cannot "style" an effect, because Style is a property of Control and an effect is not a Control.

What you really want to do is put the effect itself into the resource dictionary and use a StaticResource reference to point to it. Something like:

<UserControl.Resources>
    <DropShadowEffect x:Key="dropShadow" BlurRadius="25" Direction="315" />
    <Style TargetType="TextBlock" x:Name="PageTabLabelStyle">
        <Setter Property="FontSize" Value="16" />
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="Foreground" Value="#EFEFEF" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Margin" Value="0, 10, 0, 10" />
        <Setter Property="Effect" Value="{StaticResource dropShadow}" />
    </Style>
</UserControl.Resources>
like image 156
KeithMahoney Avatar answered Oct 11 '22 09:10

KeithMahoney