• 大小: 548KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: C#
  • 标签: WPF  DateTimePick  

资源简介

一个可以实现年月日时分秒的WPF控件,希望对学习WPF的朋友有所帮助!

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace DateTimePickerControl
{
    public partial class DateTimePicker : UserControl INotifyPropertyChanged
    {
        public DateTimePicker()
        {
            InitializeComponent();

            DatePickerObj.SelectedDate = DateTime.Now;
            this.SendPropertyChanged(“SelectedDateTimeString“);

            _selectedHour = DateTime.Now.Hour;
            _selectedMinute = DateTime.Now.Minute;
            _selectedSecond = DateTime.Now.Second;

            _oldTextBox = _currentTextBox = TextBox_Seconds;
            _currentTextBox.Background = Brushes.Pink;
        }
        public DateTime SelectedDateTime
        {
            get
            {
                DateTime obj = DatePickerObj.SelectedDate ?? DateTime.Now;
                return obj.Add(new TimeSpan(SelectedHour - obj.Hour SelectedMinute - obj.Minute SelectedSecond - obj.Second));
            }
        }
        public String SelectedDateTimeString
        {
            get
            {
                return SelectedDateTime.ToString(“yyyy-MM-dd HH-mm-ss“);
            }
        }

        #region private methods
        private void TextBox_Hours_GotFocus(object sender RoutedEventArgs e)
        {
            _oldTextBox.Background = Brushes.Transparent;
            _oldTextBox = _currentTextBox = TextBox_Hours;
            _currentTextBox.Background = Brushes.Pink;
        }
        private void TextBox_Minutes_GotFocus(object sender RoutedEventArgs e)
        {
            _oldTextBox.Background = Brushes.Transparent;
            _oldTextBox = _currentTextBox = TextBox_Minutes;
            _currentTextBox.Background = Brushes.Pink;
        }
        private void TextBox_Seconds_GotFocus(object sender RoutedEventArgs e)
        {
            _oldTextBox.Background = Brushes.Transparent;
            _oldTextBox = _currentTextBox = TextBox_Seconds;
            _currentTextBox.Background = Brushes.Pink;
        }
        private void ComboBox_DropDownClosed(object sender EventArgs e)
        {
            this.SendPropertyChanged(“SelectedDateTime“);
            this.SendPropertyChanged(“SelectedDateTimeString“);
        }

        private void BTN_IncreaseTime_Click(object sender RoutedEventArgs e)
        {
            Int32 result;
            if (Int32.TryParse(_currentTextBox.Text out result))
            {
                result++;
                _currentTextBox.Text = result.ToString();
            }
        }
        private void BTN_DecrementTime_Click(object sender RoutedEventArgs e)
   

评论

共有 条评论