Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  2. <CalendarView Name="MyCalendarView" SelectedDatesChanged="MyCalendarView_SelectedDatesChanged" CalendarViewDayItemChanging="MyCalendarView_CalendarViewDayItemChanging"></CalendarView>
  3. </Grid>
  4.  
  5. public sealed partial class MainPage : Page
  6. {
  7. public MainPage()
  8. {
  9. this.InitializeComponent();
  10. times = new ObservableCollection<DayItem>();
  11. for (int i = 1; i < 10; i++)
  12. {
  13. times.Add(new DayItem(new DateTime(2017, 1, i), Colors.Yellow));
  14. }
  15.  
  16. for (int i = 10; i < 20; i++)
  17. {
  18. times.Add(new DayItem(new DateTime(2016, 12, i), Colors.Green));
  19. }
  20. for (int i = 20; i < 29; i++)
  21. {
  22. times.Add(new DayItem(new DateTime(2017, 1, i), Colors.Red));
  23. }
  24. }
  25.  
  26. private ObservableCollection<DayItem> times;
  27.  
  28. private void MyCalendarView_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
  29. {
  30. bool add = false;
  31. //If we double click the CalendarViewDayItem, the second time args.AddedDates.Count is 0
  32. if (args.AddedDates.Count >= 1)
  33. {
  34. var newDate = args.AddedDates[0];
  35. for (int i = 0; i < times.Count; i++)
  36. {
  37. if (newDate.Date.Year == times[i].MyDateTime.Year && newDate.Date.Month == times[i].MyDateTime.Month && newDate.Date.Day == times[i].MyDateTime.Day)
  38. {
  39. add = true;
  40. }
  41. }
  42. if (!add)
  43. {
  44. times.Add(new DayItem(newDate.DateTime, Colors.Green));
  45. }
  46. }
  47. }
  48.  
  49. private void MyCalendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
  50. {
  51. var selectDay = args.Item as CalendarViewDayItem;
  52. for (int i = 0; i < times.Count; i++)
  53. {
  54. if (selectDay.Date.Year == times[i].MyDateTime.Year && selectDay.Date.Month == times[i].MyDateTime.Month && selectDay.Date.Day == times[i].MyDateTime.Day)
  55.  
  56. selectDay.Background = new SolidColorBrush(times[i].MyColor);
  57. }
  58. }
  59. }
  60.  
  61. internal class DayItem
  62. {
  63. public DayItem(DateTime today, Color red)
  64. {
  65. MyDateTime = today;
  66. MyColor = red;
  67. }
  68.  
  69. public Color MyColor { get; set; }
  70. public DateTime MyDateTime { get; set; }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement