RepeatButton Overview

The RepeatButton class represents a control that is similar to a Button. However, repeat buttons give you control over when and how the Click event occurs. The RepeatButton fires its Click event repeatedly from the time it is pressed until it is released. The Delay property determines when the event begins. You can also control the interval of the repetitions with the Interval property. This topic introduces the RepeatButton control in Windows Presentation Foundation (WPF) and illustrates how to use it in Extensible Application Markup Language (XAML) and C#.

This topic contains the following sections.

  • RepeatButton Control
  • Creating RepeatButtons
  • RepeatButton Styling
  • Related Topics

RepeatButton Control

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 following graphic shows an example of the three states of a repeat button control, Default, PointerFocused, and Pressed. The first button shows the default state of the RepeatButton. The second shows how the appearance of the button changes when the mouse pointer hovers over the button giving it focus. The last button shows the appearance of the RepeatButton when the user presses the mouse button.over the control.


Typical RepeatButton

Repeat button states

Creating RepeatButtons

The following example generates two repeat buttons, Increase and Decrease, and a button that initially contains the number 0 (zero).

<RepeatButton Width="100" DockPanel.Dock="Top" Delay="500" Interval="100" 
    Click="Increase">Increase</RepeatButton>
<RepeatButton Width="100" DockPanel.Dock="Top" Delay="500" Interval="100" 
    Click="Decrease">Decrease</RepeatButton>
<Button Name="btn" Width="100" DockPanel.Dock="Top" FontSize="16">0</Button>

After creating the buttons, you can add an event handler to handle the Click events. Event handlers must be written in a procedural programming language, such as C# or Microsoft Visual Basic. The following event handler is written in C#.

void Increase(object sender, RoutedEventArgs e)
{
    Int32 Num = Convert.ToInt32(btn.Content);

    btn.Content = ((Num + 1).ToString());
}
        
void Decrease(object sender, RoutedEventArgs e)
{
    Int32 Num = Convert.ToInt32(btn.Content);

    btn.Content = ((Num - 1).ToString());
}

For the complete see Repeat Buttons Sample.

The following example shows how to generate a RepeatButton in C#. The example creates a RepeatButton (Increase) and adds the button as a child of a Canvas element (cv3).

rpbtn = new RepeatButton();
rpbtn.Width = 50;
rpbtn.HorizontalAlignment = HorizontalAlignment.Left;
rpbtn.Width = (100);
rpbtn.Height = (25);
rpbtn.Content = "Increase";
rpbtn.Delay = (100);
rpbtn.Interval = (50);
spanel2.Children.Add(rpbtn);

For the complete sample see Creating Controls Sample.

RepeatButton Styling

With control styling, you can dramatically change the appearance and behavior of RepeatButton controls without having to write a custom control. In addition to setting visual properties, you can also apply Style elements to individual parts of a control, change the behavior of parts of the control through properties, add additional parts, and change the layout of a control. The following examples demonstrate several ways to add styles to RepeatButton controls.

The first code example defines a style called Simple that shows how to use the current system settings in your style. The code assigns the color of the ControlLightBrush as the button's background color and the ControlDarkBrush as the button's foreground color.

<Style x:Key="Simple" TargetType="{x:Type RepeatButton}">
    <Setter Property = "Background" Value= "{DynamicResource 
         {x:Static SystemColors.ControlLightBrushKey}}"/>
    <Setter Property = "Foreground" Value= "{DynamicResource 
         {x:Static SystemColors.ControlDarkBrushKey}}"/>
    <Setter Property = "FontSize" Value= "14"/>
</Style>

This sample uses triggers that enable you to change the appearance of a RepeatButton in response to events that occur on the button. When you move the mouse over the button its appearance changes.

<Style x:Key="Triggers" TargetType="{x:Type RepeatButton}">
    <Style.Triggers>
    <Trigger Property="Button.IsMouseOver" Value="true">
        <Setter Property = "Foreground" Value="Green"/>
    <Setter Property = "Background" Value="Red"/>
        <Setter Property = "FontSize" Value="16"/>
    <Setter Property = "FontStyle" Value="Italic"/>
    </Trigger>
    </Style.Triggers>
</Style>

For the complete sample see RepeatButton Styles Sample.

See Also

Tasks

How to: Use RepeatButton to Raise Events Repeatedly

Other Resources

WPF Controls Gallery Sample