了解WPF控件:RadioButton和RepeatButton常用属性与用法(九)

掌握WPF控件:熟练常用属性(九)

RadioButton

  • 一种允许用户在一组选项中单选一个的控件。通常用于提供一组互斥的选项供用户选择。
常用属性描述
Content用于设置 RadioButton 显示的文本内容。
GroupName用于将多个 RadioButton 控件组合到一组中,同一组内的 RadioButton 是互斥的,用户只能选择其中一个。不同组的 RadioButton 控件不具有互斥的关系。
Checked用于获取或设置是否选中,选中为true否则为false。
  • 下面 写个例子
 <Grid>
     <Grid.ColumnDefinitions>
         <ColumnDefinition></ColumnDefinition>
         <ColumnDefinition></ColumnDefinition>
     </Grid.ColumnDefinitions>
     <!--这里没有设置分组-->
     <GroupBox  Grid.Column="0" HorizontalAlignment="Right"  Header="这里是单选只有一组" Height="200">
         <StackPanel>
             <RadioButton Content="红色" Foreground="Red" Margin="0,5,0,5"></RadioButton>
             <RadioButton Content="橙色"  Foreground="Orange" Margin="0,5,0,5"></RadioButton>
             <RadioButton Content="绿色"  Foreground="Green"  Margin="0,5,0,5"></RadioButton>
             <RadioButton Content="蓝色"  Foreground="Blue" Margin="0,5,0,5"></RadioButton>
             <RadioButton Content="靛色"  Foreground="Indigo" Margin="0,5,0,5"></RadioButton>
             <RadioButton Content="紫色"  Foreground="Violet" Margin="0,5,0,5"></RadioButton>
         </StackPanel>
     </GroupBox>
      
      <!--这里设置分组-->
     <GroupBox  Grid.Column="1" HorizontalAlignment="Left" Margin="10,0,0,0"  Header="这是有分组单选" Height="200">
         <StackPanel>
             <RadioButton GroupName="oneGroup"  Content="红色" Foreground="Red" Margin="0,5,0,5"></RadioButton>
             <RadioButton GroupName="oneGroup" Content="橙色"  Foreground="Orange" Margin="0,5,0,5"></RadioButton>
             <RadioButton GroupName="oneGroup" Content="绿色"  Foreground="Green"  Margin="0,5,0,5"></RadioButton>
             <Separator/>
             <RadioButton GroupName="twoGroup" Content="蓝色"  Foreground="Blue" Margin="0,5,0,5"></RadioButton>
             <RadioButton GroupName="twoGroup" Content="靛色"  Foreground="Indigo" Margin="0,5,0,5"></RadioButton>
             <RadioButton  GroupName="twoGroup" Content="紫色"  Foreground="Violet" Margin="0,5,0,5"></RadioButton>
         </StackPanel>
     </GroupBox>
 </Grid>

RadioButton

RepeatButton

  • 是一个特殊的按钮,用于在用户长按或连续点击时重复执行特定动作。它通常用于需要重复执行某个操作的场景。
常用属性描述
Delay用于获取或设置 RepeatButton 在开始重复之前被按下时等待的时间(以毫秒为单位)。 该值必须为非负数。
Interval用于获取或设置开始重复后重复之间的时间间隔(以毫秒为单位)。 该值必须为非负数。
  • 下面写个例子
<Grid>
    <StackPanel Margin="0,20,0,0">

        <TextBlock   Width="350"  Margin="0,20,0,20"  TextAlignment="Center" Foreground="Red"  FontSize="16" Text="注意查看延迟和重复效果需要鼠标按住按钮不放"></TextBlock>

        <!--第一个按钮设置点击延迟500毫秒触发,触发后100毫秒执行事件一次-->
        <RepeatButton Width="100"   Delay="500" Interval="100"  Height="30" Background="Blue"  Foreground="White" Click="Increase">增加 1 </RepeatButton>

        <TextBlock x:Name="myTextBlock"  Width="100"  Margin="0,10,0,10"  TextAlignment="Center" FontSize="16">0</TextBlock>

        <!--第二个按钮也设置点击延迟500毫秒触发,触发后100毫秒执行事件一次-->
        <RepeatButton Width="100"   Delay="500" Interval="100"  Height="30" Background="Green"  Foreground="White"  Click="Decrease">减少 1</RepeatButton>

        <TextBlock x:Name="myViewTextBlock"  Width="350"  Margin="0,20,0,20"  TextAlignment="Center" Foreground="Orange"  FontSize="16" Text=""></TextBlock>


    </StackPanel>
    
</Grid>
using System.Windows;

namespace WpfCommonControls
{
    /// <summary>
    /// RepeatButton.xaml 的交互逻辑
    /// </summary>
    public partial class RepeatButton : Window
    {
        private DateTime? lastClickTime = null;//定义一个变量上次时间
        public RepeatButton()
        {
            InitializeComponent();
        }

        
        private void Increase(object sender, RoutedEventArgs e)
        {
            var currentTime = DateTime.Now;
            if (lastClickTime.HasValue)
            {
                var intervalElapsed = currentTime - lastClickTime.Value;
                myViewTextBlock.Text = $"\n上次时间 {intervalElapsed.TotalMilliseconds} 毫秒前。";
            }
            else
            {
                myViewTextBlock.Text = "首次点击";
            }
            lastClickTime = currentTime;
            //增加
            myTextBlock.Text = $"{Convert.ToInt32(myTextBlock.Text.ToString()) + 1}";
        }

        private void Decrease(object sender, RoutedEventArgs e)
        {
            var currentTime = DateTime.Now;
            if (lastClickTime.HasValue)
            {
                var intervalElapsed = currentTime - lastClickTime.Value;
                myViewTextBlock.Text = $"上次时间 {intervalElapsed.TotalMilliseconds} 毫秒前。";
            }
            else
            {
                myViewTextBlock.Text = "首次点击";
            }
            lastClickTime = currentTime;
            //减少
            int num = Convert.ToInt32(myTextBlock.Text.ToString());
            if (num > 0)
                myTextBlock.Text = $"{num - 1}";
        }
    }
}

RepeatButton

公众号“点滴分享技术猿

关注

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的工人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值