WPF 自定义DateTimePicker控件,实现时间设置与选择

        想做一个定时器功能,需要用到DateTimePicker控件。可是发现WPF只有一个设置日期的DatePicker控件,没有设置时间的DateTimePicker控件。这样很不方便,所以就自己做了一个,代码相对简洁,功能可以再做优化。另外考虑到扩展和UI体验,这里创建的是usercontrol。

先看看效果:


前台代码:

    
        
            
                
                    
                    
                    
                    
                    
                    
                
                
                :
                
                :
                
                
                    
                        
                        
                    
                    
                    
                
            
        
    



后台代码:

    public partial class DateTimePicker : UserControl
    {
        #region 重要变量

        #endregion

        #region 构造函数
        /// 
        /// 构造函数
        /// 
        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 业务处理函数
        /// 
        /// 更改选中状态
        /// 
        /// 
        /// 
        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;
                }
            }
        }

        /// 
        /// 向上加时
        /// 
        /// 
        /// 
        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();
            }
        }

        /// 
        /// 向下减时
        /// 
        /// 
        /// 
        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();
            }
        }

        /// 
        /// 初始化参数设置
        /// 
        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];
        }

        /// 
        /// 数字标准化处理
        /// 
        /// 
        /// 
        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;
                }
            }
        }

        /// 
        /// 判断是否为数字,是--->true,否--->false
        /// 
        /// 
        /// 
        private bool isNum(string str)
        {
            bool ret = true;
            foreach (char c in str)
            {
                if ((c < 48) || (c > 57))
                {
                    return false;
                }
            }

            return ret;
        }

        #endregion

    }


你可能感兴趣的:(.net(C#,winform,WPF))