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

        想做一个定时器功能,需要用到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

    }


阅读更多

没有更多推荐了,返回首页