public void DisplayCalendar(int year, int month, int day) { DisplayCalendar(year, month); DateTime dt = new DateTime(year, month, 1); dt = dt.AddDays(day - 1); CalendarListBox.SelectedIndex = dt.Day - 1; CalendarListBox.Focus(); }
private void addEvent(object sender, RoutedEventArgs e) { //[email protected] = Visibility.Visible; ////_evento = TextBox2.Text; //successAction(sender, e); try { calendar.Add(new Event((DateTime)EventDate.SelectedDate, EventText.Text)); calendar = new ObservableCollection <Event>(calendar.OrderBy(calendar => calendar)); CalendarListBox.InvalidateArrange(); CalendarListBox.ItemsSource = calendar; CalendarListBox.UpdateLayout(); } catch (Exception) { } }
public void DisplayCalendar(int year, int month) { string[] lunarTerms = new string[2] { "", "" }; int dayNum = DateTime.DaysInMonth(year, month); CalendarListBox.BeginInit(); CalendarListBox.Items.Clear(); _calendarDisplayUniformGrid = GetCalendarUniformGrid(CalendarListBox); DateTime dt = new DateTime(year, month, 1); _calendarDisplayUniformGrid.FirstColumn = (int)(dt.DayOfWeek); string sql = $"SELECT * FROM Diary WHERE RecordDate >= '{dt:yyyy-MM-dd}' AND RecordDate < '{dt.AddMonths(1):yyyy-MM-dd}'"; _diaries = new ObservableCollection <Diary>(new DiaryDAL().GetDiaries(sql)); for (int i = 0; i < dayNum; i++) { TextBlock mainDateLabel = new TextBlock { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Background = Brushes.Black, Padding = new Thickness(0, 0, 0, 0), Margin = new Thickness(0, 0, 0, 0) }; //This label is used to hold the holiday string. Label hiddenLabel = new Label { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, Visibility = Visibility.Collapsed }; //If the application is not running in zh-CN env, //it can display the date number bigger. mainDateLabel.FontSize = (_localCultureInfo.ToString() == "zh-CN") ? 20 : 25; //计算西历转换农历 UltraCalendar uc = new UltraCalendar(dt); string sLunarDate = uc.CLunarDay; if (uc.LunarDay == 1) { sLunarDate = uc.CLunarMonth + "月"; } //判断该日期是否为农历节气,是则显示该节气 string sTmp = uc.SolarTerm; if (sTmp != string.Empty) { sLunarDate = sTmp; //如为节气则农历日显示节气 sTmp = "【" + sTmp + "】" + (uc.SolarDay > 10 ? "" : " ") + uc.SolarDay.ToString() + "日"; if (lunarTerms[0] == string.Empty) { lunarTerms[0] = sTmp; } else { lunarTerms[1] = sTmp; } } //Weekend should be dispaly in red color. hiddenLabel.Content = uc.IsFestival ? uc.FestivalName : string.Empty; mainDateLabel.Foreground = GetCellBrush(uc.Weekday, uc); mainDateLabel.Text = dt.Day.ToString(NumberFormatInfo.CurrentInfo); //If the application is running in a non zh-CN locale, display no lunar calendar. Label subsidiary = null; if (_localCultureInfo.ToString() == "zh-CN") { subsidiary = new Label { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Background = Brushes.Black, Padding = new Thickness(0, 0, 0, 0), FontSize = 13, //Control the festival date to be red. Foreground = GetCellBrush(uc.Weekday, uc), Content = sLunarDate }; } //Compose the final displaying unit. StackPanel stackPanel = new StackPanel { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }; stackPanel.Children.Add(hiddenLabel); stackPanel.Children.Add(mainDateLabel); if (subsidiary != null) { stackPanel.Children.Add(subsidiary); } Diary diary = _diaries.FirstOrDefault(a => a.RecordDate >= dt.Date && a.RecordDate < dt.Date.AddDays(1)); if (diary != null) //See if having a diary or note { mainDateLabel.TextDecorations = GetUnderlineDecoration(); stackPanel.ToolTip = new ToolTip(); ToolTip tt = (ToolTip)ToolTipService.GetToolTip(stackPanel); tt.HasDropShadow = false; tt.BorderThickness = new Thickness(0); tt.Background = Brushes.Transparent; FancyToolTip toolTip = new FancyToolTip { Title = diary.Title, InfoText = diary.Content, Footer = string.Format("{0:yyyy-MM-dd HH:mm}", diary.RowVersion) }; tt.Content = toolTip; } Border border = new Border { Margin = new Thickness(1.5), Child = stackPanel }; CalendarListBox.Items.Add(border); //Display the current day in another color if (dt.Date == DateTime.Now.Date) { border.BorderThickness = new Thickness(1); border.BorderBrush = Brushes.Orange; } else if (uc.IsFestival) { border.BorderThickness = new Thickness(1); border.CornerRadius = new CornerRadius(2); border.BorderBrush = Brushes.DarkGray; } dt = dt.AddDays(1); } CalendarListBox.EndInit(); this.text8.Text = string.Format("{0}\n{1}", lunarTerms[0], lunarTerms[1]); }