views:

802

answers:

1

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?

+2  A: 

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>
KeithMahoney
Perfect, thanks Keith. Probably also worth mentioning the DropShadowEffect declaration must appear before it is referenced (in case that wasn't immediately obvious!).
Troy Hunt