Blog

Silverlight Custom Button Templates

In order to override the default look, feel and state effects of the built in Silverlight Button control you will first need to either edit the default template for the button or else create a brand new template. The quickest way to accomplish this is to use Expression Blend, which is a Microsoft Applicaiton included in the Expression Studio software suite but which can also be purchased separately.

(Creating a new Silverlight Application Project is not covered here)

Once you have added the button, to which you want to apply a custom look and feel, to the page move to the ‘Objects and Timeline’ window to the left of your screen. Select the new button by double clicking it in the object list. Right click it, go to ‘Edit Control Parts (Template)’ << see image 'Button Template Edit Menu‘>> , in the flyout menu there will be various options for editing the button’s template.  The options are as follows:

  1. Edit Template –  allows you to edit the template that is currently associated with the button. Opens the template for editing in the main window.
  2. Edit a Copy – allows you to edit a copy of the template currently associated with the button. Opens the ‘Create Resource’ dialog allowing you to choose where the new template resource will be located*.
  3. Create Empty – allows you to create a completely empty template for the button. Opens the ‘Create Resource’ dialog allowing you to choose where the new template resource will be located*.
  4. Apply Resource – allows you to apply an already existing template to the button. Opens a drop down list displaying all the existing template which can be applied to the button.

*The ‘Create Resource’ dialog <<see image 'Template Resource Location‘>> allows you to give the new template resource a unique name by which it can be referenced. It also allows you to decide if the template is local to the current page you are working in or if it should be global to the entire applicaiton. If you select local the template is created in the XAML of the page you are working in and can only be applied to buttons on that page. If you select Appliation (global) the template is then created in the App.xaml file and it is now available to all pages in the applicaiton and can be applied to any number of buttons.

Once you have decided what type of template you are going to use (existing, copy of existing, empty) and decided where it will be located in the applicaiton the workspace opens up to the template XAML and you can begin adding your custom images or vectors to the button and its various states. As you can see in the ‘Objects and Timeline’ window the buttons default states are already there and waiting with the ‘base’ (this is the state the button is in before any user interaction) already selected.

A simple button with just 2 states, base and MouseOver would contain just 2 images. With the base state selected go ahead and add two image controls and point each of them to their associated state images, making sure that the MouseOver image is the top most. once they are aligned and sized to display correctly go ahead and set the opacity value to 0% for the MouseOver image. <<see image 'Button Template Edit States‘>>

To make the MouseOver effect select the MouseOver state in the State window, it should appear white highlighted <<see image 'Button Template State Recording‘>>. You will now see the red box appear around the workspace notifying you that the state recording is on. Select the MouseOver image and set its opacity to 100% to make it visible. Save your changes and then click back to the page you were working on. Run the project (f5) and move your mouse on and off the button to see the results.

Button Template Edit Menu
Button Template Edit Menu
Template Resource Location
Template Resource Location
Button Template Edit States
Button Template Edit States
Button Template State Recording
Button Template State Recording

Visual Studio Plugins I Use

These are the Visual Studio plugins I use on a daily basis.

Code Refactoring Plugins:

ReSharper: (cost) the best code refactoring plugin for Visual Studio I have ever seen.

AutoCode: (free) never used it but its a free alternative to the expensive ReSharper.

CodeRush Express: (free) similar in function to ReSharper, for free its a pretty good tool.

Other Plugins:

GhostDoc: Free Visual Studio plug-in which adds a keyboard shortcut for adding auto generated documentation comments to your classes and methods.

CodeKeep: (free) online source code snippet repository which has a Visual Studio plugin for easy saving.

CodePlex Snippets: (free) not a plugin but a ton of Visual Studio code snippets which show up in your intellisense for quick code generation of structures you often use.

CopySourceAsHTML 3.0: (free) allows one to copy source code from Visual Studio and paste it into their blogs as html retaining the source code styles.

SmartPaster: allows you to select how you want to insert items off the clipboard (comment, region, etc.)

SnippetDesigner: assists you in creating your own Visual Studio code snippets for auto code creation.

Development Tools I Use

Applications I find extremely useful.

SlickRun: Command line utility which utilizes magic words you define to open your applications.

7-Zip: Free Open-source file compression software. Handles every compression format you need. (zip, 7-zip, rar, iso, etc.)

Agent Ransack: Free File content search utility. Searches all files of a type you specify, within a directory you specify, for a string you specify

Axialis IconWorshop Lite: Free Visual Studio 2008 plug-in allowing you to created application icons right in visual studio.

CamStudio: Free Open-Source screen capture utility. Allows you to capture video of everything happening on your screen or a selected section or window.

CCleaner: Free Computer cleaner / Registry Cleaner. Helps keep your computer clean of temp files and removes the clutter in the registry that builds up over time.

Royal TS: Free Windows Terminal Services manager. Allows you to categorize and save all of your remote desktop connection accounts in an encrypted format. Allows you to open different terminal sessions in different windows within the same application.

FileZilla: Free FTP Server/Client

FreeMind: Free Mind mapping software, helps you get our the structure of your thoughts in an organized thought tree.

KDiff3: Free File Compare Utility

Notepadd++: Free Universal note pad handling tons of languages, tons of plug-ins, very customizable

TortoiseSVN: Free Windows Explorer integrated SVN client.

TrueCrypt: Free Open-source Disk Encryption Utility.

Unlocker: Free Allows you to free files and folders which have been locked by another application without restarting your system.

IETester: Free Allows you to test web pages in multiple versions of IE (5.5, 6, 7, 8 beta) just by opening a new tab for that specific rendering engine.

Digsby: Free Messenger client which also handles email, and various social network messaging systems as well.

SimpLite: Free MSN Messenger protocol encryption software, works with many of the mainstream messaging clients as well as options for configuring non-standard ones as well.

Audacity: Free audio capture and editor for windows.

.Net Reflector: Free .Net assembly decompiler, allows you to see all the members within a given .Net compiled assembly (dll)

Expresso: Free regular expression tester.

Smart Defrag: Free real-time disk defragmentor, pretty much all the features of expensive defrag software like

DNN Adding Module Help

To add help text to the automatically created ‘Help’ link which DNN adds to each of your custom modules you simply need to add a new ‘Resource Key’ to the control’s .RESX file. For example you may have a View.aspx control which is the default display control for your module. Simply click on the designer surface in visual studio for that control, go to Tools > Generate Local Resource. A new .RESX file will be created in your module’s ‘App_LocalResources’ folder. In this file add a new key ‘ModuleHelp.Text’ and set its text value to the text you want to show up in the help control when it opens. The value you give this key can be plain text or html.

CSS Sprites, No Flash Rollovers, Reduced Bandwidth

To reduce the unsightly flashes that happen when 2 images are used for control (button) states (hover, press) in a HTML page just put the images together in one file and use CSS to move the background image to the appropriate state location within the image. This method has been labeled CSS Sprites by web developers and is extremely effective for removing the flash. But what is even more important is it speeds up your site in several ways.

Code Example:

Sprite Image
Sprite Image

div#noflash {width:200px; height:30px; background:transparent url(../Images/BlackWhiteSprite.jpg) no-repeat top left; }

div#noflash:hover {background:transparent url(../Images/BlackWhiteSprite.jpg) no-repeat -200px 0px;}

The code above shows a white square that is 200px wide and 30px high by default. Once it is hovered over by a user’s mouse instead of loading a completely different image for the hover state we simply take the same image, already in cache, and we just move it to the right -200px to reveal the black portion of the image. That is all there is to it, you can move a background side to side, top to bottom and a mixture of both to get to any x/y location within the image.

You may think that many smaller files reduce your visitor’s wait for a page to load, but you are mistaken. Your individual image sizes may be really small but part of your image’s size is meta-data describing the image (size, type, etc.). This meta data adds bulk to every image you create. To test this for yourself you can take a folder full of images and examine its properties in windows by right clicking it. When the properties window opens record the size of the folders contents. Then take all those images and in your favorite image editor and combine them all into one large image by tiling them. Now examine the new image’s properties and you will find that the combined image’s size is 80-90% smaller than the folder containing all the individual images. This is because there is only one set of meta data for the one combination image, instead of a set for each individual image.

Of course this does not mean you should take all the images in your site and create one monolithic sprite image, that could end up being massive and would really slow down the loading of your pages. What it does mean is that you should take related images, such as button states, icons, etc. and create sets out of them. Then combine the related images in each set creating a single sprite image for each of the sets. This allows one image to be loaded by the browser, cached and then used for n+ images.

How else does this method improve your site’s performance? Glad you asked! It reduces the # of Http requests that have to be made by a client’s browser to get the content needed to display a page. Why does this matter? Each Http request is a round trip from the client’s computer to the server on which the web site lives. Each resource (css, javascript, html, Images, etc.) that makes up the web page requires an individual request. The more requests that are required to build the page the longer it takes.

Lets say that a request from your browser to this server takes an average of 39ms and there are 35 images used in this web page. Now excluding the variable lag created by different sized images which can affect the response time the total response time base off the average for all of those images to reach your browser is 1365ms. Now lets say that those 35 images could be combined into 6 related sets the response time is now 234ms almost 6 times shorter.

Now of course your image is bigger now that you combined them, but remember that the overall size by combining has been reduced. Smaller size less time. So not only did we reduce the overall sice of all the images that have to be sent across the internet but we also reduced the number of requests that have to be made. Your page just loaded faster! Your visitors are thanking you! Your traffic just increased! Your bandwidth useage just went down! Your hosting bill just decreased! All I see here are benefits…