WPF篇(9)-CheckBox复选框+RadioButton单选框+RepeatButton重复按钮

CheckBox复选框

CheckBox继承于ToggleButton,而ToggleButton继承于ButtonBase基类。

案例

前端代码

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="今晚吃什么菜?" Margin="5"/>
        <CheckBox Name="_checkbox1" Content="红烧牛肉" Margin="5"/>
        <CheckBox Name="_checkbox2" Content="麻婆豆腐" Margin="5"/>
        <CheckBox Name="_checkbox3" Content="夫妻肺片" Margin="5"/>
        <Button x:Name="_button" Content="查看菜单"  Click="_button_Click"/>
    </StackPanel>

后端代码

  private void _button_Click(object sender, RoutedEventArgs e)
        {
            string order = string.Empty;
            if (_checkbox1.IsChecked == true)
                order += _checkbox1.Content + ",";
            if (_checkbox2.IsChecked == true)
                order += _checkbox2.Content + ",";
            if (_checkbox3.IsChecked == true)
                order += _checkbox3.Content;
            if(!string.IsNullOrEmpty(order))
                MessageBox.Show(order);
        }

在这里插入图片描述

我们通过判断CheckBox的IsChecked属性,来获取前端用户的选择。

RadioButton单选框

RadioButton也继承于ToggleButton,作用是单项选择,所以被称为单选框。
要注意:
这个控件有一个重要属性叫GroupName——分组名称。默认值是一个空字符串。用来指定哪些RadioButton之间是互相排斥的。

案例

前端代码

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="今晚吃什么菜?" Margin="5"/>
        <RadioButton Name="_RadioButton1" Content="红烧牛肉" Margin="5"/>
        <RadioButton Name="_RadioButton2" Content="麻婆豆腐" Margin="5"/>
        <RadioButton Name="_RadioButton3" Content="夫妻肺片" Margin="5"/>
        <Button x:Name="_button" Content="查看菜单"  Click="_button_Click"/>
    </StackPanel>

后端代码

        private void _button_Click(object sender, RoutedEventArgs e)
        {
            string order = string.Empty;
            if (_RadioButton1.IsChecked == true)
                order += _RadioButton1.Content + ",";
            if (_RadioButton2.IsChecked == true)
                order += _RadioButton2.Content + ",";
            if (_RadioButton3.IsChecked == true)
                order += _RadioButton3.Content;
            if(!string.IsNullOrEmpty(order))
                MessageBox.Show(order);
        }

在这里插入图片描述

这时候我们发现无论怎么选就只能选一个。如果我们要求必须选择一荤一素怎么办?我们看看组的使用。

前端代码

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="今晚吃什么菜?" Margin="5"/>
        <RadioButton Name="_RadioButton1" Content="红烧牛肉" GroupName="荤菜" Margin="5"/>
        <RadioButton Name="_RadioButton2" Content="糖醋排骨" GroupName="荤菜" Margin="5"/>
        <RadioButton Name="_RadioButton3" Content="麻婆豆腐" GroupName="素菜" Margin="5"/>
        <RadioButton Name="_RadioButton4" Content="清炒时蔬" GroupName="素菜" Margin="5"/>
        <Button x:Name="_button" Content="查看菜单"  Click="_button_Click"/>
    </StackPanel>

后端代码

private void _button_Click(object sender, RoutedEventArgs e)
{
    string order = string.Empty;
    if (_RadioButton1.IsChecked == true)
        order += _RadioButton1.Content + ",";
    if (_RadioButton2.IsChecked == true)
        order += _RadioButton2.Content + ",";
    if (_RadioButton3.IsChecked == true)
        order += _RadioButton3.Content + ",";
    if (_RadioButton4.IsChecked == true)
        order += _RadioButton4.Content;
    if (!string.IsNullOrEmpty(order))
        MessageBox.Show(order);
}

在这里插入图片描述

此时再操作时我们发现,红烧牛肉和糖醋排骨只能二选一,麻婆豆腐和清炒时蔬也只能二选一。

RepeatButton重复按钮

RepeatButton,顾名思义,重复执行的按钮。就是当按钮被按下时,所订阅的回调函数会不断被执行。那么,多长时间执行一次?

我们先看看它的结构定义:

public class RepeatButton : ButtonBase
{
    public static readonly DependencyProperty DelayProperty;
    public static readonly DependencyProperty IntervalProperty;
 
    public RepeatButton();
 
    public int Delay { get; set; }
    public int Interval { get; set; }
 
    protected override void OnClick();
    protected override AutomationPeer OnCreateAutomationPeer();
    protected override void OnKeyDown(KeyEventArgs e);
    protected override void OnKeyUp(KeyEventArgs e);
    protected override void OnLostMouseCapture(MouseEventArgs e);
    protected override void OnMouseEnter(MouseEventArgs e);
    protected override void OnMouseLeave(MouseEventArgs e);
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e);
    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e);
 
}

1. 属性分析

RepeatButton 自身提供了两个整型属性,分别是Delay 和Interval 。

  • Delay 属性:表示延时重复执行的毫秒数,就是说,RepeatButton被按下后会立即执行一次回调函数,如果您不松开鼠标,在等待Delay 毫秒后,就开始进行重复执行阶段。
  • Interval 属性:表示重复执行回调函数的时间间隔毫秒数

2. RepeatButton 应用示例

前端代码

  <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="食用油准备好" Margin="5 7 5 5"/>
        <RepeatButton Name="_Button1" Content="开始倒油" Delay="1000" Interval="500" Click="_Button1_Click"  Margin="5"/>
    </StackPanel>

后端代码

public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        int count = 1;
        private void _Button1_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine($"重复时间:{DateTime.Now.ToLongTimeString()} {DateTime.Now.Millisecond},重复次数:{count++}");
        }
    }

在这里插入图片描述
结果显示,第一次和第二次输出时间刚好为1000毫秒,也就是Delay属性在起作用。然后,从第2次开始,每两次之间的时间间隔大约为500毫秒,这是因为Interval属性被设置为500。

  • 17
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现带全选复选框的列表控件,可以使用WPF中的ListView控件和GridViewColumnHeader控件来实现。 首先,在ListView中添加一个CheckBox列作为全选复选框,可以使用GridViewColumnHeader控件来实现。在该控件的Click事件中,可以遍历ListView中的所有数据项,并将其对应的CheckBox控件的IsChecked属性设置为GridViewColumnHeader的IsChecked属性值。 以下是一个简单的示例代码: ```xml <ListView> <ListView.View> <GridView> <GridViewColumn> <GridViewColumnHeader Click="GridViewColumnHeader_Click"> <CheckBox x:Name="chkSelectAll" /> </GridViewColumnHeader> </GridViewColumn> <GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Header="年龄" DisplayMemberBinding="{Binding Age}" /> <GridViewColumn Header="性别" DisplayMemberBinding="{Binding Gender}" /> </GridView> </ListView.View> </ListView> ``` ```csharp private void GridViewColumnHeader_Click(object sender, RoutedEventArgs e) { foreach (var item in listView.Items) { var container = listView.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem; var checkBox = FindVisualChild<CheckBox>(container); checkBox.IsChecked = chkSelectAll.IsChecked; } } private T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject { if (obj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { var child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is T) return (T)child; var childOfChild = FindVisualChild<T>(child); if (childOfChild != null) return childOfChild; } } return null; } ``` 在上面的示例代码中,FindVisualChild方法用于查找ListViewItem中的CheckBox控件。可以将该方法封装成一个扩展方法,以便在其他地方重复使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

技术闲聊DD

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

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

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

打赏作者

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

抵扣说明:

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

余额充值