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

打印 上一主题 下一主题

主题 532|帖子 532|积分 1596

CheckBox复选框

CheckBox继续于ToggleButton,而ToggleButton继续于ButtonBase基类。
案例

前端代码
  1.     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
  2.         <TextBlock Text="今晚吃什么菜?" Margin="5"/>
  3.         <CheckBox Name="_checkbox1" Content="红烧牛肉" Margin="5"/>
  4.         <CheckBox Name="_checkbox2" Content="麻婆豆腐" Margin="5"/>
  5.         <CheckBox Name="_checkbox3" Content="夫妻肺片" Margin="5"/>
  6.         <Button x:Name="_button" Content="查看菜单"  Click="_button_Click"/>
  7.     </StackPanel>
复制代码
后端代码
  1.   private void _button_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             string order = string.Empty;
  4.             if (_checkbox1.IsChecked == true)
  5.                 order += _checkbox1.Content + ",";
  6.             if (_checkbox2.IsChecked == true)
  7.                 order += _checkbox2.Content + ",";
  8.             if (_checkbox3.IsChecked == true)
  9.                 order += _checkbox3.Content;
  10.             if(!string.IsNullOrEmpty(order))
  11.                 MessageBox.Show(order);
  12.         }
复制代码

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

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

前端代码
  1.     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
  2.         <TextBlock Text="今晚吃什么菜?" Margin="5"/>
  3.         <RadioButton Name="_RadioButton1" Content="红烧牛肉" Margin="5"/>
  4.         <RadioButton Name="_RadioButton2" Content="麻婆豆腐" Margin="5"/>
  5.         <RadioButton Name="_RadioButton3" Content="夫妻肺片" Margin="5"/>
  6.         <Button x:Name="_button" Content="查看菜单"  Click="_button_Click"/>
  7.     </StackPanel>
复制代码
后端代码
  1.         private void _button_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             string order = string.Empty;
  4.             if (_RadioButton1.IsChecked == true)
  5.                 order += _RadioButton1.Content + ",";
  6.             if (_RadioButton2.IsChecked == true)
  7.                 order += _RadioButton2.Content + ",";
  8.             if (_RadioButton3.IsChecked == true)
  9.                 order += _RadioButton3.Content;
  10.             if(!string.IsNullOrEmpty(order))
  11.                 MessageBox.Show(order);
  12.         }
复制代码

这时候我们发现无论怎么选就只能选一个。如果我们要求必须选择一荤一素怎么办?我们看看组的利用。
前端代码
  1.     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
  2.         <TextBlock Text="今晚吃什么菜?" Margin="5"/>
  3.         <RadioButton Name="_RadioButton1" Content="红烧牛肉" GroupName="荤菜" Margin="5"/>
  4.         <RadioButton Name="_RadioButton2" Content="糖醋排骨" GroupName="荤菜" Margin="5"/>
  5.         <RadioButton Name="_RadioButton3" Content="麻婆豆腐" GroupName="素菜" Margin="5"/>
  6.         <RadioButton Name="_RadioButton4" Content="清炒时蔬" GroupName="素菜" Margin="5"/>
  7.         <Button x:Name="_button" Content="查看菜单"  Click="_button_Click"/>
  8.     </StackPanel>
复制代码
后端代码
  1. private void _button_Click(object sender, RoutedEventArgs e)
  2. {
  3.     string order = string.Empty;
  4.     if (_RadioButton1.IsChecked == true)
  5.         order += _RadioButton1.Content + ",";
  6.     if (_RadioButton2.IsChecked == true)
  7.         order += _RadioButton2.Content + ",";
  8.     if (_RadioButton3.IsChecked == true)
  9.         order += _RadioButton3.Content + ",";
  10.     if (_RadioButton4.IsChecked == true)
  11.         order += _RadioButton4.Content;
  12.     if (!string.IsNullOrEmpty(order))
  13.         MessageBox.Show(order);
  14. }
复制代码

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

RepeatButton,顾名思义,重复执行的按钮。就是当按钮被按下时,所订阅的回调函数会不停被执行。那么,多长时间执行一次?
我们先看看它的结构界说:
  1. public class RepeatButton : ButtonBase
  2. {
  3.     public static readonly DependencyProperty DelayProperty;
  4.     public static readonly DependencyProperty IntervalProperty;
  5.     public RepeatButton();
  6.     public int Delay { get; set; }
  7.     public int Interval { get; set; }
  8.     protected override void OnClick();
  9.     protected override AutomationPeer OnCreateAutomationPeer();
  10.     protected override void OnKeyDown(KeyEventArgs e);
  11.     protected override void OnKeyUp(KeyEventArgs e);
  12.     protected override void OnLostMouseCapture(MouseEventArgs e);
  13.     protected override void OnMouseEnter(MouseEventArgs e);
  14.     protected override void OnMouseLeave(MouseEventArgs e);
  15.     protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e);
  16.     protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e);
  17. }
复制代码
1. 属性分析

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


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

前端代码
  1.   <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
  2.         <TextBlock Text="食用油准备好" Margin="5 7 5 5"/>
  3.         <RepeatButton Name="_Button1" Content="开始倒油" Delay="1000" Interval="500" Click="_Button1_Click"  Margin="5"/>
  4.     </StackPanel>
复制代码
后端代码
  1. public partial class MainWindow
  2.     {
  3.         public MainWindow()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.         int count = 1;
  8.         private void _Button1_Click(object sender, RoutedEventArgs e)
  9.         {
  10.             Console.WriteLine($"重复时间:{DateTime.Now.ToLongTimeString()} {DateTime.Now.Millisecond},重复次数:{count++}");
  11.         }
  12.     }
复制代码

效果表现,第一次和第二次输出时间刚好为1000毫秒,也就是Delay属性在起作用。然后,从第2次开始,每两次之间的时间隔断约莫为500毫秒,这是由于Interval属性被设置为500。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美丽的神话

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表