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

Button Controls in Silverlight

(0 votes)
Nikolay Raychev
>
Nikolay Raychev
Joined Mar 28, 2008
Articles:   22
Comments:   58
More Articles
7 comments   /   posted on May 11, 2008
Categories:   Controls

This article is compatible with the latest version of Silverlight.

Introduction

Button controls are Silverlight controls which in the common scenario respond in some way to the user who is clicking on them. There are a lot of button controls: Button, HyperlinkButton, RepeatButton, ToggleButton, CheckBox, RadioButton.

See also:
Button Article
HyperlinkButton Article
ToggleButton Article
RepeatButton Article

Overview

Four button controls derive from the base ButtonBase class: Button, HyperlinkButton, RepeatButton, and ToggleButton. The ToggleButton is a base class for the CheckBox and RadioButton controls but it can also be used as a standalone control. There are two descendants of the Button class named CalendarButton and CalendarDayButton.

The most important member of the ButtonBase class is the Click event. It occurs when a user clicks on the button control. For example:

XAML:

<UserControl x:Class="ButtonControls.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. When you set it to Release or omit it, 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 press 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> 

There are some other useful properties:

The IsFocused property gets a value indicating whether the control is focused.

The IsMouseOver property determines if the mouse pointer is over the control.

The IsPressed property indicates if the Button is currently pressed.

Conclusion

This article covers the key features of the ButtonBase class which are inherited in all button controls. 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.primitives.buttonbase(VS.95).aspx


Subscribe

Comments

  • -_-

    RE: Button Controls in Silverlight 2


    posted by harish on Nov 06, 2008 00:30

    how can we change the Ispressed property value in silverlight 2?

  • nikolayraychev

    RE: Button Controls in Silverlight 2


    posted by nikolayraychev on Nov 06, 2008 07:45

    Hi harish,

    The IsPressed property is read only so you can't change it with code.

  • -_-

    Help !!


    posted by Angelo Santos on May 25, 2009 11:19
    Can anyone tell me how to exectute a method while the LeftmouseButton is pressed ... and stop exectuting when the LeftMouseButton is up ?  thank you
  • -_-

    RE: Button Controls in Silverlight 2


    posted by Angelo Santos on May 26, 2009 04:49
    ok forget it ... i created a timer ...that solved the question ...than you anyway  :) 
  • nikolayraychev

    RE: Button Controls in Silverlight 2


    posted by nikolayraychev on May 26, 2009 06:28

    Angelo,

    Every UIElement has a MouseLeftButtonDown and MouseLeftButtonUp events. You can attach to them.

    Additionally you can use a RepeatButton if you want an event to be fired during the mouse is pressed.

  • -_-

    Button click event is firing twice


    posted by Subrat Kumar Parichha on Jun 10, 2010 12:46

    Hello,I am having the button click event in my code throgh an event delegate.But the strange thing is that it gets fired twice when i clicked it just once.Some posts and suggessions have mentioned that e.handled=true will resolve the issue.But that also didnt fix my problem.I am in a kind of stuck to it.CAN ANY BODY PLEASE HELP?

        

     

  • -_-

    RE: Button Controls in Silverlight


    posted by meow on Mar 06, 2011 11:36

    what the code for add button

    and also delete button

Add Comment

Login to comment:
  *      *