| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- 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;
- namespace ivf_tl_Operate.CustomUserControls
- {
- /// <summary>
- /// CustomerDateControl.xaml 的交互逻辑
- /// </summary>
- public partial class CustomerDateControl : UserControl
- {
- public event Action<string> SelectDateAction;
- public event Action<object> CloseAction;
- private string selectDay;
- private bool isToady = false;
- private int currentDay = 1;
- private int currentMouth = 1;
- private int currentYear;
- private DateUtil dataUtil = new DateUtil();
- private int defaultColumn = 7;
- private int row = 0;
- private int index = 0;
- private int currentMouthDay = 0;
- private int CalcCount = 1;
- private DateTime cuurentDate = DateTime.Now;
- private int[] hours = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
- private int[] mins = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 };
- public bool isNYR { get; set; }
- /// <summary>
- /// 是否可以选择未来
- /// </summary>
- public bool isSelectWeiLai { get; set; } = false;
- public CustomerDateControl()
- {
- var currentDate = DateTime.Now;
- InitializeComponent();
- combH.ItemsSource = hours;
- combH.SelectedIndex = 0;
- combM.ItemsSource = mins;
- combM.SelectedIndex = 0;
- InitCurrentDate(currentDate);
- Init(currentDate);
- btnLeft.MouseLeftButtonDown += BtnLeft_MouseLeftButtonDown;
- btnRight.MouseLeftButtonDown += BtnRight_MouseLeftButtonDown;
- btnConfrim.MouseLeftButtonDown += BtnConfrim_MouseLeftButtonDown;
- }
- public void SetNYR(bool isnyr)
- {
- isNYR = isnyr;
- if (isNYR)
- {
- c1.Visibility = Visibility.Hidden;
- }
- else
- {
- c1.Visibility = Visibility.Visible;
- }
- }
- private void BtnConfrim_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- ReturnDate();
- }
- private void ReturnDate()
- {
- string selectDate = "";
- if (!isNYR)
- {
- selectDate = string.Format("{0}-{1} {2}", tbYear.Text, selectDay.PadLeft(2, '0'), combH.SelectedValue.ToString().PadLeft(2, '0') + ":" + combM.SelectedValue.ToString().PadLeft(2, '0'));//+ ":00"
- }
- else
- {
- selectDate = string.Format("{0}-{1}", tbYear.Text, selectDay.PadLeft(2, '0'));
- }
- SelectDateAction?.Invoke(selectDate);
- }
- private void BtnRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- cuurentDate = cuurentDate.AddMonths(1);
- Init(cuurentDate);
- }
- private void InitCurrentDate(DateTime currentDate)
- {
- currentDay = currentDate.Day;
- currentMouth = currentDate.Month;
- currentYear = currentDate.Year;
- selectDay = currentDay + "";
- }
- private void CheckIsToday(DateTime date)
- {
- isToady = (date.Year == currentYear && date.Month == currentMouth && date.Day == currentDay);
- }
- private void BtnLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- cuurentDate = cuurentDate.AddMonths(-1);
- Init(cuurentDate);
- }
- public void SetSelectTime(DateTime currentDate)
- {
- InitCurrentDate(currentDate);
- Init(currentDate);
- }
- /// <summary>
- /// 可以选择未来
- /// </summary>
- public void SetSelectWeiLai()
- {
- var currentDate = DateTime.Now;
- isSelectWeiLai = true;
- InitCurrentDate(currentDate);
- Init(currentDate);
- }
- private void Init(DateTime now)
- {
- tbYear.Text = now.ToString("yyyy-MM");
- CheckIsToday(now);
- week.Children.Clear();
- InitParameter(now);
- InitGrid();
- InitWeek();
- }
- private void ClearStyle()
- {
- foreach (var item in week.Children)
- {
- TextBlock tb = item as TextBlock;
- if (tb != null)
- {
- if (DateTime.TryParse($"{this.tbYear.Text}-{tb.Text}", out DateTime itemDatetime))
- {
- if (!isSelectWeiLai)
- {
- if (itemDatetime > DateTime.Now)
- {
- }
- else
- {
- ReSetSelected(tb);
- tb = null;
- }
- }
- else
- {
- ReSetSelected(tb);
- tb = null;
- }
- }
- }
- }
- }
- private void InitRows(int rowCount)
- {
- while (rowCount-- > 0)
- {
- RowDefinition rd = new RowDefinition();
- rd.Height = new GridLength();
- week.RowDefinitions.Add(rd);
- }
- }
- private void InitColumns(int colCount)
- {
- while (colCount-- > 0)
- {
- ColumnDefinition rd = new ColumnDefinition();
- rd.Width = new GridLength();
- week.ColumnDefinitions.Add(rd);
- }
- }
- private void InitParameter(DateTime now)
- {
- index = dataUtil.CurrentMonthFristDayOfWeek(now) - 1;
- currentMouthDay = dataUtil.CurrentMonthCount(now);
- int temp = currentMouthDay - (7 - index);
- row = 1 + temp / 7 + (temp % 7 == 0 ? 0 : 1);
- }
- private void InitGrid()
- {
- InitRows(row);
- InitColumns(defaultColumn);
- }
- private void ReSetSelected(TextBlock d)
- {
- d.Background = new SolidColorBrush(Colors.White);
- d.Foreground = new SolidColorBrush(Colors.Black);
- }
- private void SetSelected(TextBlock d)
- {
- d.Background = new SolidColorBrush(Colors.Black);
- d.Foreground = new SolidColorBrush(Colors.White);
- selectDay = d.Text;
- }
- private void InitWeek()
- {
- for (int i = 0; i < row; i++)
- {
- for (int j = 0; j < defaultColumn; j++)
- {
- TextBlock d = new TextBlock();
- d.Foreground = new SolidColorBrush(Colors.Black);
- d.Width = week.Width / defaultColumn;
- d.Height = week.Height / row;
- if (!isSelectWeiLai)
- {
- if (DateTime.TryParse($"{this.tbYear.Text}-{CalcCount}", out DateTime itemDatetime))
- {
- if (itemDatetime > DateTime.Now)
- {
- d.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#B0B4B9"));
- goto CC;
- }
- }
- }
- //d.MouseEnter += (o, e) => {
- // SetSelected(d);
- //};
- //d.MouseLeave += (o, e) => {
- // ReSetSelected(d);
- //};
- d.MouseLeftButtonDown += (o, e) =>
- {
- TextBlock tb = o as TextBlock;
- if (o != null)
- {
- ClearStyle();
- SetSelected(tb);
- //if (isNYR)
- //{
- // Visibility = Visibility.Hidden;
- // ReturnDate();
- //}
- }
- };
- CC:
- d.FontSize = 60;
- d.Text = CalcCount.ToString();
- d.HorizontalAlignment = HorizontalAlignment.Center;
- d.VerticalAlignment = VerticalAlignment.Center;
- d.TextAlignment = TextAlignment.Center;
- d.SetValue(Grid.RowProperty, i);
- if (i != 0)
- {
- index = j;
- d.SetValue(Grid.ColumnProperty, index);
- }
- else
- {
- d.SetValue(Grid.ColumnProperty, index);
- if (index == 7)
- break;
- index++;
- }
- if (CalcCount == currentDay && isToady)//当天设置背景
- SetSelected(d);
- week.Children.Add(d);
- if (CalcCount == currentMouthDay)
- {
- CalcCount = 1;
- break;
- }
- CalcCount++;
- }
- }
- }
- private void Cancel_MouseUp(object sender, MouseButtonEventArgs e)
- {
- CloseAction?.Invoke(sender);
- }
- private void OkButton_MouseUp(object sender, MouseButtonEventArgs e)
- {
- ReturnDate();
- }
- private void StartDish_Click(object sender, RoutedEventArgs e)
- {
- ReturnDate();
- }
- private void Cancel_Click(object sender, RoutedEventArgs e)
- {
- CloseAction?.Invoke(sender);
- }
- }
- public class DateUtil
- {
- /// <summary>
- /// 当前月第一天是周几
- /// </summary>
- /// <returns>周几</returns>
- public int CurrentMonthFristDayOfWeek()
- {
- DateTime now = DateTime.Now;
- DateTime d1 = new DateTime(now.Year, now.Month, 1);
- int day = (int)d1.DayOfWeek;
- if (day == 0)
- day = 7;
- return day;
- }
- /// <summary>
- /// 当前月有几天
- /// </summary>
- /// <returns></returns>
- public int CurrentMonthCount()
- {
- DateTime now = DateTime.Now;
- int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
- return days;
- }
- /// <summary>
- /// 当前月有几天
- /// </summary>
- /// <param name="now">当前时间</param>
- /// <returns></returns>
- public int CurrentMonthCount(DateTime now)
- {
- int days = DateTime.DaysInMonth(now.Year, now.Month);
- return days;
- }
- /// <summary>
- /// 当前月第一天是周几
- /// </summary>
- /// <returns>周几</returns>
- public int CurrentMonthFristDayOfWeek(DateTime now)
- {
- DateTime d1 = new DateTime(now.Year, now.Month, 1);
- int day = (int)d1.DayOfWeek;
- if (day == 0)
- day = 7;
- return day;
- }
- public static long ConvertDataTimeLong(DateTime dt)
- {
- DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- TimeSpan toNow = dt.Subtract(dtStart);
- long timeStamp = toNow.Ticks;
- timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
- return timeStamp;
- }
- }
- }
|