.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share

 Subscribe to Articles

Create Button Control Template in Silverlight

Posted By :Diptimaya Patra      Posted Date :13/03/2010   Points :25   Category: Silverlight    URL: http://dpatra.blogspot.com

Create Button Control Template in Silverlight application Using Blend 3.
 


You'll start off by creating a new Silverlight Application project in Visual Studio 2008:
  1. Open Visual Studio 2008.
     
  2. Select Silverlight Application from the Visual Studio installed templates.
     
  3. Enter a project name of ControlTemplateProject (see Figure 1-1) and click OK.

    image1.gif

    Figure 1-1. Creating a new Silverlight Application project called ControlTemplateProject in Visual Studio 2008

    In this project, you are going to create a ControlTemplate for a Silverlight Button control. You are then going to create a few Silverlight Buttons and apply your new ControlTemplate to them to see how simple it is to reuse a ControlTemplate resource. So start by opening your new project in both Visual Studio 2008 as well as Blend.
     
  4. Open Blend 3 and navigate to where you saved your project, and then open the .sln (solution) file.

    Now you should have your project open in both Visual Studio 2008 and Blend 3. As you probably know by now, this is the typical workflow for me, as I like to do my design work in Blend 3 and then use Visual Studio to create my functionality (EventHandlers, etc.). With that in mind, you'll use Blend 3 to design your new Button ControlTemplate.
     
  5. Give yourself a little breathing room by selecting [UserControl] in the Objects and Timeline panel (see Figure 1-2).

    image2.gif

    Figure 1-2 Selecting [UserControl] in the Objects and Timeline panel
     
  6. Change the Height and Width of the [Window] to 600 by 600 in the Layout section of the Properties panel (see Figure 1-3)

    image3.gif

    Figure 1-3. Changing the Width and Height settings to 600
     
  7. Select the Rectangle tool in the toolbar, and draw a Rectangle control in the Workspace. Use the radius handles to give your new Rectangle rounded edges (see Figure 1-4).

    image4.gif

    Figure 1-4. Using the radius handles to give the Rectangle rounded edges

    Now you have the basis for what will be your Button control. But it does look a little simple, does it not? Go ahead and give it a quick gradient.
  8. Make sure the Rectangle is selected, and then in the Brushes section of the Properties panel, click Fill and select a Gradient brush (see Figure 1-5).

    image5.gif

    Figure 1-5. Giving your Rectangle a Gradient brush fills in the Brushes section of the Properties panel

    Go ahead and play with the gradient fill colors as well as the Brush Transformation tool until you have something you are pleased with or something like I have done, as shown in Figure 1-6.

    image6.gif

    Figure 1-6. I created this gradient fill by adjusting the colors in the Brushes section of the Properties panel and by adjusting the gradient with the Brush Transformation tool

    Now that you have a Rectangle that you are happy with, you need to tell Blend that you want to use this as a ControlTemplate for a Button. The easiest way to do this is by using Blend's Make a Button feature as follows:
     
  9. Make sure your Rectangle is selected, and then click Tools -> Make a Button.
     
  10. Blend 3 will ask you to name your new control. Name it RedButtonControl and then click OK.

    Now let's see what Blend 3 has done for you. The first thing to notice is that the Rectangle now has letters over it that read "Button" (see Figure 1-7).

    image7.gif

    Figure 1-7. When you turn your Rectangle into a Buttoncontrol, Blend places text over your Rectangle.

    What Blend also did was turn the Rectangle in the Objects and Timeline panel into a Button control (see Figure 1-8)-very cool!

    image8.gif

    Figure 1-8. Blend turns your Rectangle into a Button control.

    So now, the newly created Button control, formerly a Rectangle, has all of the capabilities of a normal Silverlight Button control, but the difference is that instead of using Silverlight's basic ButtonControlTemplate, it uses yours. Pretty cool, huh? You'll give your new Button a little functionality to prove it is just like any other Silverlight standard Button control, but before you do, I think it would be good to delve into the XAML and see exactly what Blend 3 did for you "under the hood," so to speak.
     
  11. Before you take a look at the XAML, give the new Button control a name. Name your Button myButton in the Name field of the Properties panel like I have done in Figure 1-9.

    image9.gif

    Figure 1-9. Naming your Button control in the Name section of the Properties panel
     
  12. Now click the XAML tab (or look in the XAML view in Blend 3's Split view) to see the XAML that Blend 3 has created for your myButton control, and you will see something very similar to, if not exactly like, what I have here:

    <UserControl
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" x:Class="ControlTemplateProject.MainPage" 
       Width="600" Height="600">
    <UserControl.Resources>
    <Style x:Key="RedButtonControl" TargetType="Button">
      <Setter Property="Template">
      <Setter.Value>
      <ControlTemplate TargetType="Button">
      <Grid>
      <vsm:VisualStateManager.VisualStateGroups>
      <vsm:VisualStateGroup x:Name="CommonStates">
      <vsm:VisualState x:Name="MouseOver"/>
      <vsm:VisualState x:Name="Normal"/>
      <vsm:VisualState x:Name="Pressed"/>
      <vsm:VisualState x:Name="Disabled"/>
      </vsm:VisualStateGroup>
      <vsm:VisualStateGroup x:Name="FocusStates">
      <vsm:VisualState x:Name="Unfocused"/>
      <vsm:VisualState x:Name="Focused"/>
      </vsm:VisualStateGroup>
      </vsm:VisualStateManager.VisualStateGroups>
      <Rectangle Stroke="#FF000000" RadiusX="26" RadiusY="26">
      <Rectangle.Fill>
      <LinearGradientBrush EndPoint="0.502,1.441" StartPoint="0.5,0">
      <GradientStop Color="#FFF00D0D"/>
      <GradientStop Color="#FFFFFFFF" Offset="1"/>
      </LinearGradientBrush>
      </Rectangle.Fill>
      </Rectangle>
    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
          </Grid>
      </ControlTemplate>
      </Setter.Value>
      </Setter>
    </Style>
    </UserControl.Resources>
      <Grid x:Name="LayoutRoot" Background="White">
        <Button x:Name="myButton" Height="52" HorizontalAlignment="Left" Margin="28,29,0,0" Style="{StaticResource RedButtonControl}" VerticalAlignment="Top" Width="193" Content="Button"/>
      </Grid>
    </UserControl>
    
Notice that in the main Grid there is only one UI FrameWork element, and that is a Button control with the x: Name of myButton.
Let's break down exactly what Blend 3 did:
  • Blend 3 created a Windows.Resources section.
  • In the Windows.Resources section, Blend created a Style for you called BlueButtonControl (the name you provided in the Make a Button dialog box).
  • Blend then created a Setter that defines a Template. Setters are how ControlTemplates set properties for the object they are defining.
  • In that Template, Blend created a ControlTemplate with the TargetType of Button.
  • In that ControlTemplate, Blend defined how the Button will be displayed by creating the following:

      -> A Grid
      -> A Rectangle
      -> A Rectangle.Fill
      -> A ContentPresenter (this is the text that reads "Button")
      -> ControlTemplate.Triggers to define how interaction is handled
     
  • Blend then bound the Style of your Button control to the DynamicResource of your RedButtonControl Style.
That's it you have successfully created the Button Control Template, try playing with it more. Enjoy Designing.


Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend