WPF基础五:UI②内容元素RepeatButton

RepeatButton

表示从按下按钮到释放按钮的时间内重复引发其 Click 事件的控件。

RepeatButton是 ContentControl ,这意味着它可以包含任何类型的单个对象 (例如字符串、图像或面板) 。 有关更多信息,请参见 ContentControl 类。

RepeatButton类表示与类似的控件 Button 。 不过,RepeatButton可让你控制 Click 事件发生的时间和方式。 将 RepeatButton Click 从按下该事件到释放该事件的时间重复引发该事件。 Delay属性确定事件开始的时间。 还可以通过 Interval 属性控制重复的间隔。

自定义 RepeatButton 控件

若要对多个 RepeatButton 控件应用相同的属性设置,请使用 Style 属性。 您可以修改 ControlTemplate 默认值,为控件指定独特的外观。 有关创建 ControlTemplate 的详细信息,请参阅 通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于 RepeatButton 的部分和状态,请参阅 RepeatButton 样式和模板

此控件的依赖属性可能由控件的默认样式设置。 如果按默认样式设置属性,则当控件出现在应用程序中时,属性可能会更改为默认值。 默认样式取决于应用程序运行时使用的桌面主题。 有关详细信息,请参阅 默认的 WPF 主题

只有视觉对象属性已存在于控件的默认模板中并且已使用 TemplateBinding 设置时,设置该属性才有效。 在通过创建 ControlTemplate 自定义现有控件的外观一文的更改控件的视觉结构部分可以找到视觉属性列表。


字段

名称备注权限

DelayProperty

标识 Delay 依赖项属性public

IntervalProperty

标识 Interval 依赖项属性public

属性

名称备注权限

Delay

 

获取或设置 RepeatButton 在开始重复之前被按下时等待的时间(以毫秒为单位)。 该值必须为非负数。get; set;

Interval

获取或设置开始重复后重复之间的时间间隔(以毫秒为单位)。 该值必须为非负数。get; set;

方法

名称备注权限
OnClick引发自动化事件,并调用基方法来引发 Click 事件。protected
OnCreateAutomationPeer提供 RepeatButtonAutomationPeer 此控件的适当实现,作为 WPF 基础结构的一部分。protected
OnKeyDown响应 KeyDown 事件。protected
OnKeyUp响应 KeyUp 事件。protected
OnLostMouseCaptureRepeatButton 失去鼠标捕获时调用。protected
OnMouseEnter在鼠标进入某个元素时报告。protected
OnMouseLeave在鼠标离开某个元素时报告。protected
OnMouseLeftButtonDown响应 MouseLeftButtonDown 事件。protected
OnMouseLeftButtonUp响应 MouseLeftButtonUp 事件。protected

XAML代码

按住按钮显示Click事件调用次数。

关于DelayInterval

按下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);
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值