private DateGrid SetDateGrid(DateTime dateTime) { var dateGrid = new DateGrid(); for (int i = 0; i < GetWeeksInMonth(dateTime); i++) { dateGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); } var nowMonthDaysInMonth = DateTime.DaysInMonth(dateTime.Year, dateTime.Month); // 지정한 연도, 달의 날짜 수 var startingDay = (int)new DateTime(dateTime.Year, dateTime.Month, 1).DayOfWeek; // 시작 요일 (월요일==1) var previousMonthDateTime = dateTime.AddMonths(-1); var previousMonthDaysInMonth = DateTime.DaysInMonth(previousMonthDateTime.Year, previousMonthDateTime.Month); var counter = 0; var nextMonthCounter = 1; for (int row = 0; row < 7; row++) { for (int col = 0; col < 7; col++) { int date = 0; var cell = new CellStackLayout { VerticalOptions = LayoutOptions.FillAndExpand, HorizontalOptions = LayoutOptions.FillAndExpand, }; var label = new Label { VerticalOptions = LayoutOptions.FillAndExpand, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalTextAlignment = TextAlignment.Center, HorizontalTextAlignment = TextAlignment.Center }; if (row == 0) { var color = Color.Black; if (col == 0) { color = Color.Red; } else if (col == 6) { color = Color.Blue; } label.Text = WeekDays[col]; label.TextColor = color; label.FontAttributes = FontAttributes.Bold; cell.Children.Add(label); dateGrid.Children.Add(cell, col, row); } else { if (counter < startingDay) { date = previousMonthDaysInMonth - (startingDay - counter - 1); label.Text = date.ToString(); label.TextColor = Color.Gray; cell.DateTimeInfo = new DateTime(dateTime.AddMonths(-1).Year, dateTime.AddMonths(-1).Month, date); //cell.Children.Add(label); //dateGrid.Children.Add(cell, col, row); } else if (counter >= startingDay && (counter - startingDay) < nowMonthDaysInMonth) { date = counter + 1 - startingDay; label.Text = date.ToString(); cell.DateTimeInfo = new DateTime(dateTime.Year, dateTime.Month, date); if (DateTime.Now.Year == dateTime.Year && DateTime.Now.Month == dateTime.Month && DateTime.Now.Day == date) { label.TextColor = Color.Accent; } else if (DateTime.Now.Year > dateTime.Year || (DateTime.Now.Year == dateTime.Year && DateTime.Now.Month > dateTime.Month) || (DateTime.Now.Year == dateTime.Year && DateTime.Now.Month == dateTime.Month && DateTime.Now.Day > date)) { label.TextColor = Color.Gray; } else { label.TextColor = Color.Black; AddTapGesture(cell); } cell.Children.Add(label); dateGrid.Children.Add(cell, col, row); } else if (counter >= (nowMonthDaysInMonth + startingDay)) { date = nextMonthCounter++; label.Text = date.ToString(); label.TextColor = Color.Gray; cell.DateTimeInfo = new DateTime(dateTime.AddMonths(+1).Year, dateTime.AddMonths(+1).Month, date); } counter++; } //cellStackLayouts.Add(cell); } } return(dateGrid); }
private void AddTapGesture(CellStackLayout cell) { cell.GestureRecognizers.Clear(); cell.GestureRecognizers.Add(_tapGestureRecognizer); }