(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Using the Button control in Silverlight

(1 votes)
Nikolay Raychev
>
Nikolay Raychev
Joined Mar 28, 2008
Articles:   22
Comments:   58
More Articles
7 comments   /   posted on May 12, 2008
Tags:   button , nikolay-raychev
Categories:   Controls

This article is compatible with the latest version of Silverlight.

Introduction

The Button is a Silverlight control which responds when the user clicks on it.

See also:
Button Controls Article
HyperlinkButton Article
ToggleButton Article
RepeatButton Article

Overview

Since the Button derives directly from the ButtonBase class and there isn’t an additional functionality, I’ll use the examples from the Button Controls Article.

The most important event of the Button, inherited from ButtonBase class is the Click event. It occurs when a user clicks on the button. For example:

XAML:

<UserControl x:Class="Button2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="200" Height="100">
    <Canvas x:Name="cnvLayoutRoot" Background="White">
        <TextBlock x:Name="tblText" Canvas.Top="20" Canvas.Left="30" Text="Not clicked yet."></TextBlock>
        <Button x:Name="btnTest" Canvas.Top="60" Canvas.Left="30" Click="btnTest_Click" Content="Click me!"></Button>
    </Canvas>
</UserControl>

Code behind:

private void btnTest_Click( object sender, RoutedEventArgs e )
{
    this.tblText.Text = "The button was clicked.";
}

We have a TextBlock and a Button. We specify the event handler for the Click event of the Button in the XAML and then write code in the code behind to respond to this event. In our example we change the Text of the TextBlock when the user clicks on the Button. It results in the following:

An interesting property is the ClickMode, also inherited from the ButtonBase class. When you set it to Release(default), the Click event occurs when you release the mouse button:

<Button ClickMode="Release" x:Name="btnTest" Canvas.Top="60" Canvas.Left="30" Click="btnTest_Click" Content="Click me!"></Button>

Setting this property to Press causes the event to occur immediately when you click the mouse button, for example:

<Button ClickMode="Press" x:Name="btnTest" Canvas.Top="60" Canvas.Left="30" Click="btnTest_Click" Content="Click me!"></Button>

Setting this property to Hover causes the event to occur when you move over the Button with the mouse:

<Button ClickMode="Hover" x:Name="btnTest" Canvas.Top="60" Canvas.Left="30" Click="btnTest_Click" Content="Click me!"></Button>

Example

Imagine that we want to have the following Button which starts a Silverlight animation:

Since the Button is a ContentControl we can use any object for its Content property. So you can put a layout control in it, containing other controls:

<UserControl x:Class="Button2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="200" Height="100">
    <Canvas x:Name="cnvLayoutRoot" Background="LightGray">
        <Button x:Name="btnTest" Background="White" Canvas.Top="30" Canvas.Left="30">
            <StackPanel x:Name="spnlButtonContent" VerticalAlignment="Center" Orientation="Horizontal">
                <Image x:Name="imgSls" Source="/Images/sls.jpg" Width="30" Height="30" />
                <TextBlock x:Name="tblPlay" Text=" PLAY " FontWeight="Bold" Foreground="#1ca0f2" VerticalAlignment="Center"/>
            </StackPanel>
        </Button>
    </Canvas>
</UserControl>

In our example we just put a StackPanel in our Button, containing an Image and a TextBlock. So you can arrange the Button’s Content depending on your desires and imagination.

Conclusion

This article is just a brief description of the key features of the Button control. It targets the developer who has just started with the Silverlight controls. Any comments are welcome.

Reference
http://msdn.microsoft.com/en-us/library/system.windows.controls.button(VS.95).aspx


Subscribe

Comments

  • -_-

    RE: Using the Button control in Silverlight 2


    posted by Dave on Jan 12, 2009 08:28

    Hi Niko,

    I am a newbie to SL and I had read one of your article and have just finished reading a second one.
    Great JOB !

    Dave

  • -_-

    RE: Using the Button control in Silverlight 2


    posted by A J A Y on Jan 31, 2009 03:41

    Always good to find Silverlight Documentation that is UNDERSTANDABLE :-)

  • -_-

    RE: Using the Button control in Silverlight 2


    posted by jundu88@hotmail.com on Mar 02, 2010 00:13

    I want my button to be able to handle both "Hover" and "Click" events. How do I do it?

    Thanks,

  • nikolayraychev

    RE: Using the Button control in Silverlight 2


    posted by nikolayraychev on Mar 02, 2010 15:46

    Hi,

    You can attach to the MouseEnter and MouseLeave events in order to handle the hover. Additionally you can make a custom control template and use the MouseOver visual state.

  • -_-

    RE: Using the Button control in Silverlight 2


    posted by Luis on Sep 24, 2010 19:02

    I want to move the button dinamicly with C# code. What property is good for this?

     

     

  • -_-

    RE: Using the Button control in Silverlight


    posted by aann on May 01, 2011 19:41

    I want to add a undo button to siverlight code help me

  • Lec

    Re: Using the Button control in Silverlight


    posted by Lec on Dec 29, 2011 02:25
    Not sure if this is the right spot ...can I ask help here? Is there any difference from Silverlight 2 button control to Silverlight 5 button control?

Add Comment

Login to comment:
  *      *