Using XAML RepeatButton in WPF

XAML RepeatButton in WPF

XAML RepeatButton represents a set of repeat buttons. This article shows how to use a RepeatButton control in WPF using XAML and C#.

Creating a RepeatButton

The RepeatButton XAML element represents a WPF RepeatButton control.

  1. <Button/>  
The Width and Height attributes represent the width and the height of a RepeatButton. The Content property sets the text of the button. The Name attribute represents the name of the control, that is a unique identifier of a control.

The code snippet in Listing 1 creates a Button control and sets its name, height, width and content.
  1. <RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"   
  2.    HorizontalAlignment="Left"   
  3.    Name="GrowButton" Width="80" Height="30">  
  4. </RepeatButton>  
Listing 1

The default property of a button is Content. The code snippet in Listing 2 creates the same button as created by Listing 1.
  1. <RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"   
  2.    HorizontalAlignment="Left"   
  3.    Name="GrowButton" Width="80" Height="30">  
  4.    Grow  
  5. </RepeatButton>  
Listing 2

The output looks as in Figure 1.



Figure 1

Delay and Interval

The Delay and Interval properties make a RepeatButton different from a normal button.

A RepeatButton is a button that fires Click events repeatedly when it is pressed and held. The rate and aspects of repeating are determined by the Delay and Interval properties that the control exposes.

The code snippet in Listing 3 sets the Delay and Interval properties.
  1. <RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"   
  2.    HorizontalAlignment="Left"   
  3.    Name="GrowButton" Width="80" Height="30"   
  4.    Delay="500" Interval="100" >  
  5.    Grow  
  6. </RepeatButton>  
Listing 3

Adding a Button Click Event Handler

The Click attribute of a RepeatButton element adds the click event handler and it keeps firing the event for the given Interval and delay values. The code in Listing 4 adds the click event handler for a Button.
  1. <Button x:Name="DrawCircleButton" Height="40" Width="120"   
  2.         Canvas.Left="10" Canvas.Top="10"   
  3.         Content="Draw Circle"  
  4.         VerticalAlignment="Top"   
  5.         HorizontalAlignment="Left">  
  6. Click="DrawCircleButton_Click"  
  7. </Button>  
Listing 4

The code for the click event handler looks as in following.
  1. private void GrowButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. }  
Okay, now let's write a useful application.

We will build an application with the two buttons Grow and Shrink and a rectangle. The application looks as in Figure 2.

When you click and continue pressing the Grow button, the width of the rectangle will continue to grow and when you click on the Shrink button, the width of the rectangle will shrink continuously.



Figure 2

The final XAML code is listed in Listing 5.
  1. <Window x:Class="RepeatButtonSample.Window1"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.     Title="Window1" Height="300" Width="300">  
  5.       
  6.     <Grid Name="LayoutRoot">  
  7.         <RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"   
  8.                       HorizontalAlignment="Left"                         
  9.                       Name="GrowButton"  Width="80" Height="30"   
  10.                       Delay="500" Interval="100"   
  11.                       Click="GrowButton_Click">  
  12.             Grow  
  13.         </RepeatButton>  
  14.         <RepeatButton Margin="100,10,0,0" VerticalAlignment="Top"   
  15.                       HorizontalAlignment="Left"                         
  16.                       Name="ShrinkButton"  Width="80" Height="30"   
  17.                       Delay="500" Interval="100"   
  18.                       Click="ShrinkButton_Click">  
  19.             Shrink  
  20.         </RepeatButton>  
  21.           
  22.         <Rectangle Name="Rect" Height="100" Width="100" Fill="Orange"/>  
  23.   
  24.     </Grid>  
  25. </Window>  
Listing 5

Listing 6
is the click event handlers for the buttons that change the width of the rectangle.
  1. private void GrowButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     Rect.Width += 10;  
  4. }  
  5.   
  6. private void ShrinkButton_Click(object sender, RoutedEventArgs e)  
  7. {  
  8.     Rect.Width -= 10;  
  9. }  
Listing 6

 

Up Next
    Ebook Download
    View all
    Learn
    View all