WPF 自定义DateTimePicker控件,实现时间设置与选择
来源:互联网 发布:淘宝立体服装拍摄姿势 编辑:程序博客网 时间:2024/06/06 01:36
想做一个定时器功能,需要用到DateTimePicker控件。可是发现WPF只有一个设置日期的DatePicker控件,没有设置时间的DateTimePicker控件。这样很不方便,所以就自己做了一个,代码相对简洁,功能可以再做优化。另外考虑到扩展和UI体验,这里创建的是usercontrol。
先看看效果:
前台代码:
<Grid> <Border BorderThickness="1" BorderBrush="Black"> <Grid > <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition Width="3"></ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition Width="3"></ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <TextBox Name="textbox_hour" BorderThickness="0" TextChanged="numtextboxchanged" SelectionChanged="textbox_hour_SelectionChanged" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Column="0"></TextBox> <TextBlock Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center">:</TextBlock> <TextBox Name="textbox_minute" BorderThickness="0" TextChanged="numtextboxchanged" SelectionChanged="textbox_hour_SelectionChanged" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Column="2"></TextBox> <TextBlock Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Center">:</TextBlock> <TextBox Name="textbox_second" BorderThickness="0" TextChanged="numtextboxchanged" SelectionChanged="textbox_hour_SelectionChanged" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Column="4"></TextBox> <Grid Grid.Column="5"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Name="button_up" Click="button_up_Click" Grid.Row="0" FontSize="5">▲</Button> <Button Name="button_down" Click="button_down_Click" Grid.Row="1" FontSize="5">▼</Button> </Grid> </Grid> </Border> </Grid>
后台代码:
public partial class DateTimePicker : UserControl { #region 重要变量 #endregion #region 构造函数 /// <summary> /// 构造函数 /// </summary> public DateTimePicker() { InitializeComponent(); this.initParameters(); this.textbox_minute.Background = Brushes.White; this.textbox_second.Background = Brushes.White; this.textbox_hour.Background = Brushes.White; } #endregion #region 业务处理函数 /// <summary> /// 更改选中状态 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textbox_hour_SelectionChanged(object sender, RoutedEventArgs e) { TextBox tb = sender as TextBox; if (tb != null) { switch (tb.Name) { case "textbox_hour": tb.Background = Brushes.Gray; this.textbox_minute.Background = Brushes.White; this.textbox_second.Background = Brushes.White; break; case "textbox_minute": tb.Background = Brushes.Gray; this.textbox_hour.Background = Brushes.White; this.textbox_second.Background = Brushes.White; break; case "textbox_second": tb.Background = Brushes.Gray; this.textbox_hour.Background = Brushes.White; this.textbox_minute.Background = Brushes.White; break; } } } /// <summary> /// 向上加时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_up_Click(object sender, RoutedEventArgs e) { if (this.textbox_hour.Background == Brushes.Gray) { int temp = System.Int32.Parse(this.textbox_hour.Text); temp++; if (temp > 24) { temp = 0; } this.textbox_hour.Text = temp.ToString(); } else if (this.textbox_minute.Background == Brushes.Gray) { int temp = System.Int32.Parse(this.textbox_minute.Text); temp++; if (temp > 60) { temp = 0; } this.textbox_minute.Text = temp.ToString(); } else if (this.textbox_second.Background == Brushes.Gray) { int temp = System.Int32.Parse(this.textbox_second.Text); temp++; if (temp > 60) { temp = 0; } this.textbox_second.Text = temp.ToString(); } } /// <summary> /// 向下减时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_down_Click(object sender, RoutedEventArgs e) { if (this.textbox_hour.Background == Brushes.Gray) { int temp = System.Int32.Parse(this.textbox_hour.Text); temp--; if (temp < 0) { temp = 24; } this.textbox_hour.Text = temp.ToString(); } else if (this.textbox_minute.Background == Brushes.Gray) { int temp = System.Int32.Parse(this.textbox_minute.Text); temp--; if (temp < 0) { temp = 60; } this.textbox_minute.Text = temp.ToString(); } else if (this.textbox_second.Background == Brushes.Gray) { int temp = System.Int32.Parse(this.textbox_second.Text); temp--; if (temp < 0) { temp = 60; } this.textbox_second.Text = temp.ToString(); } } /// <summary> /// 初始化参数设置 /// </summary> private void initParameters() { string strt = System.DateTime.Now.ToString("HH:mm:ss"); this.textbox_hour.Text = strt.Split(':')[0]; this.textbox_minute.Text = strt.Split(':')[1]; this.textbox_second.Text = strt.Split(':')[2]; } /// <summary> /// 数字标准化处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void numtextboxchanged(object sender, TextChangedEventArgs e) { TextBox tb = sender as TextBox; if (tb != null) { if ((this.isNum(tb.Text) == false) || (tb.Text.Length > 2)) { tb.Text = "0"; MessageBox.Show("请输入正确的时间!", "警告!"); return; } } } /// <summary> /// 判断是否为数字,是--->true,否--->false /// </summary> /// <param name="str"></param> /// <returns></returns> private bool isNum(string str) { bool ret = true; foreach (char c in str) { if ((c < 48) || (c > 57)) { return false; } } return ret; } #endregion }
阅读全文
0 0
- WPF 自定义DateTimePicker控件,实现时间设置与选择
- DateTimePicker控件进行时间选择与显示
- Silverlight DateTimePicker 自定义的日期及时间选择控件
- jquery datetimepicker 时间选择控件
- 时间日期选择控件DateTimePicker
- WPF自定义控件选择
- DateTimePicker控件中自定义时间格式
- datetimepicker实现时间选择的相互制约
- wpf 自定义时间编辑控件
- [WinForm]DateTimePicker选择时间
- bootstrap-datetimepicker 时间选择
- bootstrap-datetimepicker:基于twitter bootstrap的日期/时间选择控件
- bootstrap-datetimepicker:基于twitter bootstrap的日期/时间选择控件
- Bootstrap时间控件 datetimepicker
- bootstrap-datetimepicker时间控件
- bootstrap-datetimepicker时间控件
- bootstrap-datetimepicker时间控件
- bootstrap-datetimepicker时间控件
- Java基础---final关键字
- 把opencv Mat 按位存成bmp二值图像 (1bit 1pixel)
- Keepalived安装/简单配置
- 干货
- Android NDK 编译PjSip 2.6 之 环境准备 (一)
- WPF 自定义DateTimePicker控件,实现时间设置与选择
- oracle用户被锁定the account is locked的解决办法
- 记录一些常见的Linux命令
- ajax页面动态判断
- 浅析Hololens HoloToolkit中Utilities下通用脚本的用法
- openfire批量推送
- io流常见实例记录
- HTML 2017.07.19
- MainActivity.java9