473,609 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fading Glowy Button for WPF and Silverlight

ThatThatGuy
449 Recognized Expert Contributor
This tutorial is intended to enlighten the power of XAML to build rich applications for Windows(WPF), Web(Silverlight ) & Browser(XBAP). I will explain to you about building attractiveFadin g Glow Buttons by using pure XAML. With XAML we don't necessarily need to build a user or a custom control for a fancy looking control. Instead we use styles, the same as CSS applied to Web Controls.

For this we'll be creating a Resource Dictionary. A Resource Dictionary is a file which contains Styles for Controls (like .css files). We define Resource Dictionaries for the sake of reusability. Although, It's not neccessary for you to create a Resource Dictionary for applying styles. You may write application level styles in App.xaml or Window or Page level styles in their respective xaml sources.

You may add a ResourceDiction ary file by Adding a new Reosurce Dictionary from Project menu.
By default your Resource Dictionary file will contain the following markup.
Expand|Select|Wrap|Line Numbers
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3.  
Now before going into styles we'll introduce this Resource file to our App.xaml
Expand|Select|Wrap|Line Numbers
  1.     <Application.Resources>
  2.         <ResourceDictionary>
  3.             <ResourceDictionary.MergedDictionaries>
  4.                 <ResourceDictionary Source="Resources/Generic.xaml"/>
  5.             </ResourceDictionary.MergedDictionaries>
  6.         </ResourceDictionary>
  7.     </Application.Resources>
  8.  
As you can see in the above markup under the <Application.Re sources> we need to Merge resource files that will be used in the application. I've kept all resource files under Resources folder, you may specify your own.

After doing this lets get back to defining styles. This is where we gonna be defining styles for the button. Now, defining Fading glowy Styles for a button takes quite a bit of a XAML markup.

So I'll be explaining each phase part by part.

Phase 1: Approach towards styles
Expand|Select|Wrap|Line Numbers
  1. <Style x:Key="GlowButtonStyle" TargetType="{x:Type Button}"> 
  2.  
At first we'll be defining a Style like this where we specify the accessible name of the style GlowButtonStyle and the the type of control we're targeting i.e. Button.

Then i specify the the text color of the button.
Expand|Select|Wrap|Line Numbers
  1. <Setter Property="Button.Foreground" Value="Black"/>
  2.  
To change the entire look and feel of the button and creating our own look and feel, we need to override the default temaplte and define a new template. That's what the most of the code is all about.

We'll be setting the new template under the control's template property
Expand|Select|Wrap|Line Numbers
  1.         <Setter Property="Template">
  2.             <Setter.Value>
  3.                 <ControlTemplate TargetType="{x:Type Button}" x:Name="ButtonUpControlTemplate">
  4.  
In the above markup we are setting a new template for the control, all the code is under the <ControlTemplat e> markup.

Now, with nothing as a template the button will be simply invisible on the window..
Phase 2: Giving a background to the Empty Button
For this we create a light border around the button to make it appear..
Expand|Select|Wrap|Line Numbers
  1.          <Border x:Name="ButtonBorder" 
  2.                   BorderThickness="1"
  3.                   BorderBrush="Gray">
  4.  
Now for the gradient part we have to paint the background gradient for the button in te border background..

After giving it a border we need provide for content settings for the button as in the Text to be held inside the button.
Expand|Select|Wrap|Line Numbers
  1.    <ContentPresenter x:Name="ButtonContentPresenter"
  2.                                 VerticalAlignment="Center"  
  3.                                 HorizontalAlignment="Center"/>
  4.  
ContentPresente r manages the Content Alignment and informs the empty button Canvas that it has to hold some content inside it. Without specifying ContentPresente r you will not be able to see ant content embedded inside the button.

Now about filling the control background.
Expand|Select|Wrap|Line Numbers
  1.                         <Border.Background>
  2.                             <LinearGradientBrush x:Name="ButtonUp"    StartPoint="0,0" EndPoint="0,1">
  3.                                 <GradientStop Color="WhiteSmoke" Offset="0.1"/>
  4.                                 <GradientStop Color="LightGray" Offset="0.2"/>
  5.                                 <GradientStop Color="White" Offset="0.9"/>
  6.                             </LinearGradientBrush>
  7.                         </Border.Background>
  8.  
You specify your gradient fill settings in Border.Backgrou nd as a part of the button background. Here I've defined a LinearGradientB rush with some better looking gradient stops and also specifying the StartPoint as origin of the gradient and EndPoint as End of the gradient.

and then finally close the Border with </Border> tag
Expand|Select|Wrap|Line Numbers
  1. </Border>
  2.  
Phase 3: Arrange for Animated Glowy Effects
Under this i'll be explaining you the animation part which makes the button glowy on mouse hover and back to normal on mouse leave.

Animated effects are best suitable to a button when mouse is over it or when mouse is clicked.
Now, to handle animation on mouse hover we need to write markup for that under <ControlTemplat e.Triggers> markup.
Expand|Select|Wrap|Line Numbers
  1.     <MultiTrigger>
  2.       <MultiTrigger.Conditions>
  3.        <Condition Property="Button.IsMouseOver" Value="True"/>
  4.         </MultiTrigger.Conditions>
  5.  
For specifying on which trigger to take the specified action we need to specfiy the trigger conditon under <MultiTrigger.C onditions> markup.

Herein we check for the property of the Button. If mouse is hovered over it and if the property value satisfies the condition. Then we write the code for the animation as under...
Expand|Select|Wrap|Line Numbers
  1.          <MultiTrigger.EnterActions>
  2.          <BeginStoryboard Storyboard="{StaticResource ButtonAnimationOver}"/>
  3.             </MultiTrigger.EnterActions>
  4.  
In <MultiTrigger.E nterActions> we start the storyboard for color Animation which references animation resource in the same file.
Expand|Select|Wrap|Line Numbers
  1.     <Storyboard x:Key="ButtonAnimationOver">
  2.             <ParallelTimeline>
  3.                 <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" From ="WhiteSmoke" To="PaleGoldenrod" Duration="0:0:0.50"/>
  4.             <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" From ="LightGray" To="PaleGoldenrod" Duration="0:0:0.50"/>
  5.                 <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[2].(GradientStop.Color)" From ="White" To="PaleGoldenrod" Duration="0:0:0.50"/>
  6.             </ParallelTimeline>
  7.     </Storyboard>
  8.  
This StoryBoard plays ColorAnimation which changes the current gradient colors of the button to PaleGoldenRed Color over a period of time specified in the Duration Property.

And when the mouse has left the control focus we write the ExitActions for the effect as below
Expand|Select|Wrap|Line Numbers
  1.         <MultiTrigger.ExitActions>
  2.          <BeginStoryboard Storyboard="{StaticResource ButtonAnimationOut}"/>
  3.             </MultiTrigger.ExitActions>
  4.  
and the exit animation
Expand|Select|Wrap|Line Numbers
  1.     <Storyboard x:Key="ButtonAnimationOut">
  2.             <ParallelTimeline>
  3.                 <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" To ="WhiteSmoke" From="PaleGoldenrod" Duration="0:0:0.50"/>
  4.             <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" To="LightGray" From="PaleGoldenrod" Duration="0:0:0.50"/>
  5.                 <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[2].(GradientStop.Color)" To="White" From="PaleGoldenrod" Duration="0:0:0.50"/>
  6.             </ParallelTimeline>
  7.     </Storyboard>
  8.  


Now having been the style ready to be applied for our button we go to the button to be styled and apply the style.
Expand|Select|Wrap|Line Numbers
  1.    <Button Content="Button4" Height="42" x:Name="button1" Width="85" Style="{StaticResource GlowButtonStyle}"/>
  2.  
We specify the style name for the button in Style Property and that's all you need to style the button to a Fading Glowy Button.
Here's the full Resource Dictionary code.
Expand|Select|Wrap|Line Numbers
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3.     <!-- Style for application Background-->
  4.     <Style x:Key="ApplicationBackground" TargetType="{x:Type Window}">
  5.         <Setter Property="Control.Background" Value="WhiteSmoke"/>
  6.     </Style>
  7.  
  8.  
  9.  
  10.     <Storyboard x:Key="ButtonAnimationOver">
  11.             <ParallelTimeline>
  12.                 <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" From ="WhiteSmoke" To="PaleGoldenrod" Duration="0:0:0.50"/>
  13.             <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" From ="LightGray" To="PaleGoldenrod" Duration="0:0:0.50"/>
  14.                 <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[2].(GradientStop.Color)" From ="White" To="PaleGoldenrod" Duration="0:0:0.50"/>
  15.             </ParallelTimeline>
  16.     </Storyboard>
  17.     <Storyboard x:Key="ButtonAnimationOut">
  18.             <ParallelTimeline>
  19.                 <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" To ="WhiteSmoke" From="PaleGoldenrod" Duration="0:0:0.50"/>
  20.             <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" To="LightGray" From="PaleGoldenrod" Duration="0:0:0.50"/>
  21.                 <ColorAnimation Storyboard.TargetName="ButtonUp" Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[2].(GradientStop.Color)" To="White" From="PaleGoldenrod" Duration="0:0:0.50"/>
  22.             </ParallelTimeline>
  23.     </Storyboard>
  24.  
  25.     <!-- Common Button Style-->
  26.     <Style x:Key="GlowButtonStyle" TargetType="{x:Type Button}"> 
  27.         <Setter Property="Button.Foreground" Value="Black"/>
  28.         <Setter Property="Template">
  29.             <Setter.Value>
  30.                 <ControlTemplate TargetType="{x:Type Button}" x:Name="ButtonUpControlTemplate">
  31.                     <Border x:Name="ButtonBorder" 
  32.                   BorderThickness="1"
  33.                   BorderBrush="Gray">
  34.                         <ContentPresenter x:Name="ButtonContentPresenter"
  35.                                 VerticalAlignment="Center"  
  36.                                 HorizontalAlignment="Center"/>
  37.                         <Border.Background>
  38.                             <LinearGradientBrush x:Name="ButtonUp"    StartPoint="0,0" EndPoint="0,1">
  39.                                 <GradientStop Color="WhiteSmoke" Offset="0.1"/>
  40.                                 <GradientStop Color="LightGray" Offset="0.2"/>
  41.                                 <GradientStop Color="White" Offset="0.9"/>
  42.                             </LinearGradientBrush>
  43.                         </Border.Background>
  44.                     </Border>
  45.                     <ControlTemplate.Triggers>
  46.                         <MultiTrigger>
  47.                             <MultiTrigger.Conditions>
  48.                                 <Condition Property="Button.IsMouseOver" Value="True"/>
  49.                             </MultiTrigger.Conditions>
  50.                             <MultiTrigger.EnterActions>
  51.                                 <BeginStoryboard Storyboard="{StaticResource ButtonAnimationOver}"/>
  52.                             </MultiTrigger.EnterActions>
  53.                             <MultiTrigger.ExitActions>
  54.                                 <BeginStoryboard Storyboard="{StaticResource ButtonAnimationOut}"/>
  55.                             </MultiTrigger.ExitActions>
  56.                         </MultiTrigger>
  57.                     </ControlTemplate.Triggers>
  58.                 </ControlTemplate>
  59.             </Setter.Value>
  60.         </Setter>
  61.     </Style>
  62. </ResourceDictionary>
  63.  
May 28 '10 #1
0 7681

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2844
by: Faisal Shafiq | last post by:
I want to upload a file direct to the Silverlight Streaming Service from a Web Client such as silverlight application. As per our product requirement we want to upload a .WMV file directly from silverlight client to Silverlight streaming service. I tried to user WebClient and HttpWebRequest for that purpose but, unfortunately I can found the way to do so. There are some problems with both classes. 1. There is no property of get...
2
6579
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click in the child datagrid and then go to the combobox and choose another vendor. When the new vendor is loaded nothing shows in the datagrid but the itemsource shows the info is there. Know if I click on the child cell and then click back on the...
3
689
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
Note: My apologies for repeating this post from last week, but my nospam alias and profile account were incorrect. I think I have fixed this, so hopefully this post will trigger MS into a response per their MSDN policy. -------------------- I have a web site under .NET 2.0 that renders videos using the Silverlight media player. The web page looks like this: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="video2.aspx.cs"
13
3985
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
This is a follow-up to my post "Silverlight video doesn't work when file is streamed from handler in ASP.net" at http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.dotnet.framework.aspnet&mid=e9a38d03-83a8-41fc-8950-5ee60d2a18a5. I have a web site under .NET 2.0 that renders videos using the Silverlight media player. When I stream the video file (.wmv) to the browser via a hard-coded link to the file,...
7
7143
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and Silverlight 2.0. To get these tools please visit this page Get Started : The Official Microsoft Silverlight Site and follow Step 1. Occasionally you find the need to have users upload multiple files at once. You could use multiple FileUpload...
3
8643
by: pavanip | last post by:
Hi, I am new to Silverlight application.I want to display video using Mediaelement tag and I used the below code to play video. <Canvas> <MediaElement x:Name="mPlayer" Width="640" Height="480" Source="a_video.avi"/> <Button x:Name="bPlay" Background="Green" Width="100" Height="45" Canvas.Left="8" Canvas.Top="497" Content="Play" /> <Button x:Name="bPause" Background="Yellow" Width="100" Height="45"...
0
5835
Curtis Rutland
by: Curtis Rutland | last post by:
Have any of you ever used a Reverse Polish Notation calculator? I did in high school. It was easily the best calculator ever (the HP=32SII). RPN is great, because you don't have to use parenthesis. The stack and the order in which you key the operators maintains order of operations. Today, I'll walk you through creating one. Note this tutorial is technically for Silverlight, but except for the marked sections, it can be applied wholesale to...
0
8076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8573
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8406
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5510
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4021
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2531
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.