This article is compatible with the latest version of Silverlight.
Introduction
The RepeatButton is a Silverlight control which does an action repeatedly from the time a user presses it till the time it is released.
See also:
Button Controls Article
Button Article
HyperlinkButton Article
ToggleButton Article
Overview
To demonstrate the common use of the RepeatButton I’ll give an example:
When we press the RepeatButton the TextBlock starts showing the current time.
XAML:
<UserControl x:Class="RepeatButton2.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>
<RepeatButton x:Name="rbtnTest" Click="rbtnTest_Click" Delay="1000" Interval="500" Canvas.Top="60" Canvas.Left="30" Content="Show the time"></RepeatButton>
</Canvas>
</UserControl>
Code behind:
private void rbtnTest_Click( object sender, RoutedEventArgs e )
{
this.tblText.Text = DateTime.Now.ToString( System.Globalization.CultureInfo.InvariantCulture );
}
The only thing we did to achieve this behavior is that we attached to the Click event, inherited from the ButtonBase class and in the event handler we set the TextBlock’s Text property to the string representation of the current time. When we release the button the TextBlock stops changing its value every second.
An important property is the Interval. Its value is in milliseconds and it determines the interval between the occurrences of the Click event while the button is being pressed. The default is 250.
The Delay property determines the time in milliseconds between the pressing of the button and the time when the button starts repeating the Click event. The default is 250.
If you want to see how you can use the ClickMode property visit the Button Controls Article. This member is inherited from the ButtonBase class. The example there is with a Button but it is the same with a RepeatButton.
Issues
Just imagine that we want the RepeatButton to wait 10 seconds before it starts showing the time. To do that we set the Delay property to 10000 like this:
<RepeatButton x:Name="rbtnTest" Click="rbtnTest_Click" Delay="10000" Interval="500" Canvas.Top="60" Canvas.Left="30" Content="Show the time"></RepeatButton>
The expected behavior is that when the button is pressed it waits 10 seconds and then it shows the time. This isn’t exactly what happens. When the button is pressed the current date and time show. A pause of 10 seconds follows and after it the TextBlock begins to update. It seems that there is one parasitic Click event. Maybe it is inherited from the ButtonBase class. Check yourself:
In our case we can find a solution, for example setting the ClickMode to Release:
<RepeatButton x:Name="rbtnTest" ClickMode="Release" Click="rbtnTest_Click" Delay="10000" Interval="500" Canvas.Top="60" Canvas.Left="30" Content="Show the time"></RepeatButton>
In this case the extra Click event will occur when the button is released:
Still, if we want the ClickMode to be Hover:
<RepeatButton x:Name="rbtnTest" ClickMode="Hover" Click="rbtnTest_Click" Delay="10000" Interval="500" Canvas.Top="60" Canvas.Left="30" Content="Show the time"></RepeatButton>
Here is the result:
Microsoft might have had something in mind when they made it that way. I just look from the point of view of the user because I am a user.
Example
Martin Mihaylov’s demo about MultiscaleImage uses repeat buttons to zoom in and out: MultiscaleImage Demo
Conclusion
This article is just a brief description of the key features of the RepeatButton 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.primitives.repeatbutton(VS.95).aspx