Esempio n. 1
0
        private void BuildItems()
        {
            if (_itemsGrid != null)
            {
                AddDefaultItems();
                var startOfMonth = new DateTime(_year, _month, 1);
                DayOfWeek dayOfWeek = startOfMonth.DayOfWeek;
                var daysInMonth = (int)Math.Floor(startOfMonth.AddMonths(1).Subtract(startOfMonth).TotalDays);
                var addedDays = 0;
                int lastWeekNumber = 0;
                for (int rowCount = 1; rowCount <= RowCount; rowCount++)
                {
                    for (var columnCount = 1; columnCount < ColumnCount; columnCount++)
                    {
                        var item = (CalendarItem)(from oneChild in _itemsGrid.Children
                                                  where oneChild is CalendarItem &&
                                                  ((CalendarItem)oneChild).Tag.ToString() == string.Concat(rowCount.ToString(), ":", columnCount.ToString())
                                                  select oneChild).First();
                        if (rowCount == 1 && columnCount < (int)dayOfWeek + 1)
                        {
                            item.Visibility = Visibility.Collapsed;
                        }
                        else if (addedDays < daysInMonth)
                        {
                            item.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            item.Visibility = Visibility.Collapsed;
                        }

                        var weekItem = (CalendarWeekItem)(from oneChild in _itemsGrid.Children
                                                          where oneChild is CalendarWeekItem &&
                                                          ((CalendarWeekItem)oneChild).Tag.ToString() == string.Concat(rowCount.ToString(), ":0")
                                                          select oneChild).FirstOrDefault();

                        if (item.Visibility == Visibility.Visible)
                        {
                            item.ItemDate = startOfMonth.AddDays(addedDays);
                            if (SelectedDate == DateTime.MinValue && item.ItemDate == DateTime.Today)
                            {
                                SelectedDate = item.ItemDate;
                                if (ShowSelectedDate)
                                    item.IsSelected = true;
                                _lastItem = item;
                            }
                            else
                            {
                                if (item.ItemDate == SelectedDate)
                                {
                                    if (ShowSelectedDate)
                                        item.IsSelected = true;
                                }
                                else
                                {
                                    item.IsSelected = false;
                                }
                            }
                            addedDays += 1;
                            item.DayNumber = addedDays;
                            item.SetBackcolor();
                            item.SetForecolor();

                            if (WeekNumberDisplay != WeekNumberDisplayOption.None)
                            {
                                int weekNumber;

                                if (WeekNumberDisplay == WeekNumberDisplayOption.WeekOfYear)
                                {
                                    var systemCalendar = System.Globalization.CultureInfo.CurrentCulture.Calendar;
                                    weekNumber = systemCalendar.GetWeekOfYear(
                                        item.ItemDate,
                                        System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
                                        System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
                                }
                                else
                                {
                                    weekNumber = rowCount;
                                }
                                if (weekItem != null)
                                {
                                    weekItem.WeekNumber = weekNumber;
                                    lastWeekNumber = weekNumber;
                                    weekItem.Visibility = Visibility.Visible;
                                }
                            }
                        }
                        else
                        {
                            if (WeekNumberDisplay != WeekNumberDisplayOption.None && weekItem != null && weekItem.WeekNumber != lastWeekNumber)
                            {
                                weekItem.Visibility = Visibility.Collapsed;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
 private void ItemClick(object sender, RoutedEventArgs e)
 {
     if (_lastItem != null)
     {
         _lastItem.IsSelected = false;
     }
     _lastItem = (sender as CalendarItem);
     if (_lastItem != null)
     {
         if (ShowSelectedDate)
             _lastItem.IsSelected = true;
         SelectedDate = _lastItem.ItemDate;
         OnDateClicked(_lastItem.ItemDate);
     }
 }
Esempio n. 3
0
 private void AddDefaultItems()
 {
     if (!_addedItems && _itemsGrid != null)
     {
         for (int rowCount = 1; rowCount <= RowCount; rowCount++)
         {
             for (int columnCount = 1; columnCount < ColumnCount; columnCount++)
             {
                 var item = new CalendarItem(this);
                 item.SetValue(Grid.RowProperty, rowCount);
                 item.SetValue(Grid.ColumnProperty, columnCount);
                 item.Visibility = Visibility.Collapsed;
                 item.Tag = string.Concat(rowCount.ToString(), ":", columnCount.ToString());
                 item.Click += ItemClick;
                 if (CalendarItemStyle != null)
                 {
                     item.Style = CalendarItemStyle;
                 }
                 _itemsGrid.Children.Add(item);
             }
             if (WeekNumberDisplay != WeekNumberDisplayOption.None)
             {
                 const int columnCount = 0;
                 var item = new CalendarWeekItem();
                 item.SetValue(Grid.RowProperty, rowCount);
                 item.SetValue(Grid.ColumnProperty, columnCount);
                 item.Visibility = Visibility.Collapsed;
                 item.Tag = string.Concat(rowCount.ToString(), ":", columnCount.ToString());
                 if (CalendarWeekItemStyle != null)
                 {
                     item.Style = CalendarWeekItemStyle;
                 }
                 _itemsGrid.Children.Add(item);
             }
         }
         _addedItems = true;
     }
 }