Click here to Skip to main content
15,884,836 members
Articles / Desktop Programming / WPF

Popup WPF Image with Drop Shadow

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
20 May 2011CPOL 23.9K   3   3
Make any image in WPF pop up and hit you in the face!

Make any image in WPF pop up and hit you in the face!

Since WPF makes it extremely easy to create and share styles for various objects in XAML, I wanted to share some of the more useful or good-looking styles that I have created.

In order to give my image-buttons a more Windows friendly look in WPF, I have created the following style:

XML
<Style TargetType="{x:Type Image}">
  <Setter Property="RenderTransform">
    <Setter.Value>
      <ScaleTransform ScaleX="1" ScaleY="1"/>
    </Setter.Value>
  </Setter>
  <Setter Property="BitmapEffect">
    <Setter.Value>
      <DropShadowBitmapEffect Color="Black" Direction="315" 
         Opacity="0" ShadowDepth="3" Softness="0" />
    </Setter.Value>
  </Setter>
  <Setter Property="RenderTransformOrigin" Value=".5,.5" />
  <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
      <Trigger.EnterActions>
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" 
               To="1.5" Duration="0:0:0.1" />
            <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY" 
               To="1.5" Duration="0:0:0.1" />
            <DoubleAnimation Storyboard.TargetProperty="BitmapEffect.Opacity" 
               To=".5" Duration="0:0:0.1" />
          </Storyboard>
        </BeginStoryboard>
      </Trigger.EnterActions>
      <Trigger.ExitActions>
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" 
               To="1" Duration="0:0:0.1" />
            <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY" 
               To="1" Duration="0:0:0.1" />
            <DoubleAnimation Storyboard.TargetProperty="BitmapEffect.Opacity" 
               To="0" Duration="0:0:0.1" />
          </Storyboard>
        </BeginStoryboard>
      </Trigger.ExitActions>
    </Trigger>
  </Style.Triggers>
</Style>

I mainly use this style on my toolbar buttons, but you can customize it so that you can use it anywhere and have it look great.

Here is a sample button in its normal state:

button-off

Here is a sample button when the mouse is over the image (with some minor modifications, you can have the image pop up when the mouse is over the button itself):

button-on

Note: This style sets the image properties for the entire Window when put in the Window.Resources section of your XAML file. If you want to style just specific images, give the style a key instead of just specifying a type. That way you can apply the style to only images of your choice:

XML
<Style x:Key="PopupImageStyle" TargetType="{x:Type Image}">    

...

XML
<Image Source="Resources/Open Multiple.ico" Style="{StaticResource PopupImageStyle}" />

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Check out my technical blog here: The Fyslexic Duck. You can find most of what I've put on CodeProject there, plus some additional technical articles.

Comments and Discussions

 
QuestionAbout for an imagen Pin
Jordan Ramírez2-Jun-15 17:23
Jordan Ramírez2-Jun-15 17:23 
GeneralNice [modified] Pin
Roberto Mazzone24-May-11 21:49
Roberto Mazzone24-May-11 21:49 
GeneralRe: Nice Pin
Aron Weiler25-May-11 6:22
Aron Weiler25-May-11 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.