RepeatButton
表示从按下按钮到释放按钮的时间内重复引发其 Click 事件的控件。
RepeatButton是 ContentControl ,这意味着它可以包含任何类型的单个对象 (例如字符串、图像或面板) 。 有关更多信息,请参见 ContentControl 类。
RepeatButton类表示与类似的控件 Button 。 不过,RepeatButton可让你控制 Click 事件发生的时间和方式。 将 RepeatButton Click 从按下该事件到释放该事件的时间重复引发该事件。 Delay属性确定事件开始的时间。 还可以通过 Interval 属性控制重复的间隔。
若要对多个 RepeatButton 控件应用相同的属性设置,请使用 Style 属性。 您可以修改 ControlTemplate 默认值,为控件指定独特的外观。 有关创建 ControlTemplate 的详细信息,请参阅 通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于 RepeatButton 的部分和状态,请参阅 RepeatButton 样式和模板。
此控件的依赖属性可能由控件的默认样式设置。 如果按默认样式设置属性,则当控件出现在应用程序中时,属性可能会更改为默认值。 默认样式取决于应用程序运行时使用的桌面主题。 有关详细信息,请参阅 默认的 WPF 主题。
只有视觉对象属性已存在于控件的默认模板中并且已使用 TemplateBinding 设置时,设置该属性才有效。 在通过创建 ControlTemplate 自定义现有控件的外观一文的更改控件的视觉结构部分可以找到视觉属性列表。
字段
名称 | 备注 | 权限 |
---|---|---|
标识 Delay 依赖项属性 | public | |
标识 Interval 依赖项属性 | public |
属性
名称 | 备注 | 权限 |
---|---|---|
| 获取或设置 RepeatButton 在开始重复之前被按下时等待的时间(以毫秒为单位)。 该值必须为非负数。 | get; set; |
获取或设置开始重复后重复之间的时间间隔(以毫秒为单位)。 该值必须为非负数。 | get; set; |
方法
名称 | 备注 | 权限 |
---|---|---|
OnClick | 引发自动化事件,并调用基方法来引发 Click 事件。 | protected |
OnCreateAutomationPeer | 提供 RepeatButtonAutomationPeer 此控件的适当实现,作为 WPF 基础结构的一部分。 | protected |
OnKeyDown | 响应 KeyDown 事件。 | protected |
OnKeyUp | 响应 KeyUp 事件。 | protected |
OnLostMouseCapture | RepeatButton 失去鼠标捕获时调用。 | protected |
OnMouseEnter | 在鼠标进入某个元素时报告。 | protected |
OnMouseLeave | 在鼠标离开某个元素时报告。 | protected |
OnMouseLeftButtonDown | 响应 MouseLeftButtonDown 事件。 | protected |
OnMouseLeftButtonUp | 响应 MouseLeftButtonUp 事件。 | protected |
XAML代码
按住按钮显示Click事件调用次数。
按下RepeatButton后,在Delay的范围时间内,松掉RepeatButton,触发一次Click事件。
按下RepeatButton后,触发一次Click事件,超过Delay时间,又触发一次Click事件,松掉RepeatButton,停止触发。
按下RepeatButton后,触发一次Click事件,超过Delay时间,又触发一次Click事件,超过Interval,还会触发Click事件,松掉RepeatButton,停止触发。如下图:
<Window x:Class="RepeatButtonDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RepeatButtonDemo"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="200">
<Grid>
<StackPanel>
<Label x:Name="lbl1" Content="0" Height="35" />
<RepeatButton Content="RepeatButton" Height="35" Delay="500" Interval="5000" Click="OnClick" ClickMode="Press"/>
</StackPanel>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RepeatButtonDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
static int count = 0;
public MainWindow()
{
InitializeComponent();
}
private void OnClick(object sender, RoutedEventArgs e)
{
lbl1.Content = (++count);
}
}
}
运行并按下按钮:
C#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RepeatButtonDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
static int count = 0;
public MainWindow()
{
InitializeComponent();
Grid grid = (this as Window).Content as Grid;
StackPanel stackPanel = new StackPanel();
Label lbl1 = new Label();
System.Windows.Controls.Primitives.RepeatButton repeatButton = new System.Windows.Controls.Primitives.RepeatButton();
lbl1.Content = count;
lbl1.Height = 35;
repeatButton.Content = "RepeatButton";
repeatButton.Height = 35;
repeatButton.Delay = 500;
repeatButton.Interval = 5000;
repeatButton.Click += OnClick;
repeatButton.ClickMode = ClickMode.Press;
stackPanel.Children.Add(lbl1);
stackPanel.Children.Add(repeatButton);
grid.Children.Add(stackPanel);
}
private void OnClick(object sender, RoutedEventArgs e)
{
foreach (var item in ((sender as System.Windows.Controls.Primitives.RepeatButton).Parent as StackPanel).Children)
{
if (item is Label) (item as Label).Content = (++count);
}
}
}
}