private void UpdateTableLayoutDays() { DateTime[,] array = CalendarArray.Generate(Month, FirstDayOfWeek); for (int row = 0; row < 6; row++) { LinearLayout tableRow = (LinearLayout)_tableLayoutDays.GetChildAt(row); for (int col = 0; col < 7; col++) { DateTime date = array[row, col]; DayType dayType; if (DateTools.SameMonth(date, Month)) { dayType = DayType.ThisMonth; } else if (date < Month) { dayType = DayType.PrevMonth; } else { dayType = DayType.NextMonth; } IMyCalendarDayView dayView = (IMyCalendarDayView)tableRow.GetChildAt(col); dayView.UpdateDay(date, dayType, date.Date == DateTime.Today); } } }
public CalendarGrid(DateTime month, DateTime?selectedDate) { this.month = month; Color backgroundColor = Colors.Purple; base.Background = new SolidColorBrush(backgroundColor); Grid inner = new Grid(); for (int i = 0; i < 7; i++) { inner.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1 / 7.0, GridUnitType.Star) }); } inner.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); for (int i = 0; i < 6; i++) { inner.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1 / 6.0, GridUnitType.Star) }); } base.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); base.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); base.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); TextBlock textBlockHeader = new TextBlock() { Text = month.ToString("MMMM yyyy"), Foreground = new SolidColorBrush(Colors.White), FontWeight = FontWeights.Bold, FontSize = 14, HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center, VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center, Margin = new Thickness(0, 4, 0, 4) }; Grid headerBackground = new Grid() { Background = new SolidColorBrush(Colors.Purple) }; Grid.SetColumn(headerBackground, 0); Grid.SetRow(headerBackground, 0); base.Children.Add(headerBackground); //Grid.SetColumn(textBlockHeader, 0); //Grid.SetRow(textBlockHeader, 0); //base.Children.Add(textBlockHeader); headerBackground.Children.Add(textBlockHeader); TextBlock left = new TextBlock() { Text = "<", Foreground = new SolidColorBrush(Colors.White), FontSize = 16, FontWeight = FontWeights.Bold, HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left, VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center, Margin = new Thickness(8, 4, 0, 4) }; left.Tapped += delegate { if (LeftClicked != null) { LeftClicked(this, new EventArgs()); } }; headerBackground.Children.Add(left); TextBlock right = new TextBlock() { Text = ">", Foreground = new SolidColorBrush(Colors.White), FontSize = 16, FontWeight = FontWeights.Bold, HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right, VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center, Margin = new Thickness(0, 4, 8, 4) }; right.Tapped += delegate { if (RightClicked != null) { RightClicked(this, new EventArgs()); } }; headerBackground.Children.Add(right); Grid.SetColumn(inner, 0); Grid.SetRow(inner, 1); base.Children.Add(inner); SolidColorBrush brushOtherMonth = new SolidColorBrush(Color.FromArgb(255, 190, 190, 190)); SolidColorBrush brushOtherMonthMouse = new SolidColorBrush(Color.FromArgb(255, 220, 220, 220)); SolidColorBrush brushToday = new SolidColorBrush(Colors.White) { Opacity = 0.3 }; SolidColorBrush brushTodayMouse = new SolidColorBrush(Colors.White) { Opacity = 0.5 }; addDay("Sun", inner, 0); addDay("Mon", inner, 1); addDay("Tue", inner, 2); addDay("Wed", inner, 3); addDay("Thu", inner, 4); addDay("Fri", inner, 5); addDay("Sat", inner, 6); DateTime[,] array = CalendarArray.Generate(month, DayOfWeek.Sunday); int index = 0; for (int col = 0; col < 7; col++) { for (int row = 0; row < 6; row++) { CalendarSquare g = null; if (array[row, col].Date == DateTime.Today) { if (selectedDate != null && array[row, col].Date == selectedDate.Value.Date) { g = new CalendarSquare(array[row, col], CalendarSquare.DisplayType.TodaySelected); } else { g = new CalendarSquare(array[row, col], CalendarSquare.DisplayType.Today); } } else if (array[row, col].Month == month.Month) { if (selectedDate != null && array[row, col].Date == selectedDate.Value.Date) { g = new CalendarSquare(array[row, col], CalendarSquare.DisplayType.ThisMonthSelected); } else { g = new CalendarSquare(array[row, col], CalendarSquare.DisplayType.ThisMonth); } } else { if (selectedDate != null && array[row, col].Date == selectedDate.Value.Date) { g = new CalendarSquare(array[row, col], CalendarSquare.DisplayType.OtherMonthSelected); } else { g = new CalendarSquare(array[row, col], CalendarSquare.DisplayType.OtherMonth); } } decorateDayGrid(g, array[row, col]); Grid.SetColumn(g, col); Grid.SetRow(g, row + 1); //first row is Mon, Tues, Wed... inner.Children.Add(g); g.Tapped += delegate { if (currSelectedGrid != null) { currSelectedGrid.Selected = false; } currSelectedGrid = g; g.Selected = true; if (SelectionChanged != null) { SelectionChanged(this, new EventArgsCalendar(g.Date)); } }; squares[index] = g; index++; } } SelectedDate = selectedDate; }
protected void Initialize() { base.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch; base.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch; Grid inner = new Grid(); for (int i = 0; i < 7; i++) { inner.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1 / 7.0, GridUnitType.Star) }); } inner.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); for (int i = 0; i < 6; i++) { inner.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1 / 6.0, GridUnitType.Star) }); } base.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); base.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); //add header FrameworkElement header = GenerateCalendarHeader(DisplayMonth); Grid.SetRow(header, 0); base.Children.Add(header); //add grid for calendar Grid.SetRow(inner, 1); base.Children.Add(inner); //add the day headers DayOfWeek dayHeader = FirstDayOfWeek; for (int i = 0; i < 7; i++, dayHeader++) { addDay(dayHeader, inner, i); } //add the days DateTime[,] array = CalendarArray.Generate(DisplayMonth, FirstDayOfWeek); for (int col = 0; col < 7; col++) { for (int row = 0; row < 6; row++) { DateTime date = array[row, col]; TCalendarSquare square = GenerateCalendarSquare(date); _calendarSquares[date] = square; if (date.Date == DateTime.Today) { square.Type = TCalendarSquare.DisplayType.Today; } else if (date.Month == DisplayMonth.Month) { square.Type = TCalendarSquare.DisplayType.ThisMonth; } else { square.Type = TCalendarSquare.DisplayType.OtherMonth; } //add extra margin for side squares //left if (col == 0) { if (row == 5) { square.Margin = new Thickness(square.Margin.Left * 2, square.Margin.Top, square.Margin.Right, square.Margin.Bottom * 2); } else { square.Margin = new Thickness(square.Margin.Left * 2, square.Margin.Top, square.Margin.Right, square.Margin.Bottom); } } //right else if (col == 6) { if (row == 5) { square.Margin = new Thickness(square.Margin.Left, square.Margin.Top, square.Margin.Right * 2, square.Margin.Bottom * 2); } else { square.Margin = new Thickness(square.Margin.Left, square.Margin.Top, square.Margin.Right * 2, square.Margin.Bottom); } } //bottom else if (row == 5) { square.Margin = new Thickness(square.Margin.Left, square.Margin.Top, square.Margin.Right, square.Margin.Bottom * 2); } Grid.SetColumn(square, col); Grid.SetRow(square, row + 1); //first row is Mon, Tues, Wed... inner.Children.Add(square); square.Tapped += square_Tapped; } } FrameworkElement overlay = GenerateOverlay(); if (overlay != null) { Grid.SetRowSpan(overlay, 2); base.Children.Add(overlay); } }