Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 8.12 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Creating events/appointments for custom calender WPF
  2. <ItemsControl.ItemTemplate>
  3.  
  4.             <DataTemplate>
  5.  
  6.                     <StackPanel>
  7.  
  8.  
  9.                         <Button Content="{Binding day}" Width="175" HorizontalAlignment="Stretch" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Name="btnCalenderDate" Click="btnCalenderDate_Click" Loaded="btnCalenderDate_Loaded" Height="18" FontSize="10" FontWeight="Bold">
  10.  
  11.                         </Button>
  12.                         <TextBlock OpacityMask="Black"  Name="txtBlockdays" VerticalAlignment="Top" TextWrapping="Wrap">
  13.  
  14.                         </TextBlock>
  15.  
  16.                     </StackPanel>
  17.                 </DataTemplate>
  18.  
  19.             </ItemsControl.ItemTemplate>
  20.  
  21.  
  22.         <!-- ItemContainerStyle -->
  23.         <ItemsControl.ItemContainerStyle>
  24.  
  25.  
  26.             <Style >
  27.  
  28.                     <Setter Property="Grid.Column" Value="{Binding WeekDay}"  />
  29.                 <Setter Property="Grid.Row" Value="{Binding WeekNo}" />
  30.  
  31.             </Style>
  32.  
  33.             </ItemsControl.ItemContainerStyle>
  34.        
  35. public partial class SchedulePage : Page, INotifyPropertyChanged
  36. {
  37.  
  38.      public event PropertyChangedEventHandler PropertyChanged;
  39.     MainWindow _parentForm;
  40.  
  41.     Schedule sched = new Schedule();
  42.     DateTime date = DateTime.Now;
  43.  
  44.  
  45.     Event newevent;
  46.  
  47.  
  48.  
  49.  
  50.  
  51.    public SchedulePage(MainWindow parentForm)
  52.     {
  53.         InitializeComponent();
  54.  
  55.         _parentForm = parentForm;
  56.  
  57.         DateTime date = DateTime.Now; //These couple of lines determine the current month to show on startup.
  58.         _numValue = date.Month;
  59.         _numYear = date.Year;
  60.         //int year = date.Year;
  61.         _nameofmonth = NumValue;
  62.         _nameofyear = NumYear;
  63.         lblcurrentdate.Content = date.Month + "/ " + date.Day + "/ " + date.Year;
  64.         makeCalender();
  65.     }
  66.  
  67. //The region below are properties that are passed in to makeCalender() when the DateRight and DateLeft buttons
  68.     // get clicked theses values change. The properties hold the month and the year. Everytime a user
  69.     // clicks forward or backward to the next month, these values equal to the selected current month & year.
  70.  
  71.     // However, Everytime these values change a new calender is made and the old one is cleared(deleted).
  72.     #region
  73.     // properties
  74.    public void NotifyPropertyChanged(String info)
  75.     {
  76.        // if (PropertyChanged != null)
  77.        // {
  78.             PropertyChanged(this, new PropertyChangedEventArgs(info));
  79.         //}
  80.     }
  81.  
  82.     private int _numValue;
  83.     public int NumValue
  84.     {
  85.         get { return _numValue; }
  86.         set
  87.         {
  88.             _numValue = value;
  89.  
  90.         }
  91.     }
  92.  
  93.     private int _numYear;
  94.     public int NumYear
  95.     {
  96.         get { return _numYear; }
  97.         set
  98.         {
  99.             _numYear = value;
  100.  
  101.         }
  102.     }
  103.  
  104.  
  105.    private int _nameofmonth ;
  106.  
  107.     public int NameofMonth
  108.     {
  109.         get
  110.         {
  111.             return this._nameofmonth;
  112.         }
  113.  
  114.         set
  115.         {
  116.             if (value != this._nameofmonth)
  117.             {
  118.                 this._nameofmonth = value;
  119.                 NotifyPropertyChanged("NameofMonth");
  120.             } return;
  121.         }
  122.     }
  123.  
  124.     private int _nameofyear;
  125.     public int NameofYear
  126.     {
  127.         get
  128.         {
  129.             return this._nameofyear;
  130.         }
  131.  
  132.         set
  133.         {
  134.             if (value != this._nameofyear)
  135.             {
  136.                 this._nameofyear = value;
  137.                 NotifyPropertyChanged("NameofYear");
  138.             } return;
  139.         }
  140.     }
  141.    #endregion //end of properties
  142.  
  143.     //void TheViewModel_PropertyChanged(object src, PropertyChangedEventArgs e)
  144.  /*    
  145.     {
  146.         _parentForm.bindings.schedule.Clear();
  147.         _nameofmonth = NumValue;//comboMonth.SelectedIndex + 1;
  148.  
  149.         _nameofyear = comboYear.SelectedIndex + 2011;
  150.  
  151.         makeCalender();
  152.  
  153.     } */
  154.  
  155.  
  156.     // makeCalender() is how the calender is created and how it determines the gridcell layout of each month.
  157.     public void makeCalender() {
  158.  
  159.         var t = new List<Schedule>();
  160.         DateTime curr = DateTime.Now;
  161.         int[] m = new int[7];
  162.        DateTime newcurr = new DateTime(NameofYear, NameofMonth, 1);
  163.  
  164.         var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
  165.         var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
  166.         for (int k = 0; k < 7; k++)
  167.         {
  168.             for (var i = 1; newcurr.Month == NameofMonth; newcurr = newcurr.AddDays(1))
  169.             {
  170.  
  171.                 var month_week = (newcurr.Day / 7);
  172.                 sched.MonthWeek = newcurr.GetWeekOfMonth().ToString();
  173.                 sched.Month = newcurr.Month.ToString();
  174.                 sched.Year = newcurr.Year.ToString();
  175.                 sched.day = newcurr.Day.ToString();
  176.                 sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
  177.                 sched.dayofweek = newcurr.DayOfWeek.ToString();
  178.                 t.Add(sched);
  179.  
  180.           //Here is where the calender is created. By looping through all the days in the selected month it will find the weeknumber and day of the week -->
  181.           // that that particular date belongs to and place in the correct gridcell.
  182.           _parentForm.bindings.schedule.Add(new Schedule { WeekNo = newcurr.GetWeekOfMonth() - 1, WeekDay = (int)newcurr.DayOfWeek, day = newcurr.Day.ToString() });
  183.  
  184.                 lblDate.Content = getMonth(newcurr.Month) + " " + newcurr.Year; //getMonth() determines string format of current month displayed.
  185.  
  186.             }
  187.         }
  188.  
  189.  
  190.         DataContext = _parentForm.bindings;
  191.  
  192.  
  193.  
  194.     }
  195.  
  196.    // btnDateRight_Click allows us to navigate forward to the next month, if current month is -->
  197.     //      december then the current year is incremented and the month starts on january (NumValue = 01)
  198.      private void btnDateRight_Click(object sender, RoutedEventArgs e)
  199.      {
  200.          _parentForm.bindings.schedule.Clear();
  201.          if (NumValue >= 1 && NumValue < 12)
  202.          {
  203.              NumValue += 1;
  204.  
  205.              _nameofmonth = NumValue;
  206.  
  207.          } else
  208.          {
  209.  
  210.              NumYear += 1;
  211.              _nameofyear = NumYear;
  212.              NumValue = 01;
  213.              _nameofmonth = NumValue;
  214.          }
  215.          makeCalender();
  216.      }
  217.  
  218.      // btnDateLeft_Click allows us navigate backwards to a previous month
  219.      //  if navigating back and the current month is january, then the year is deincremented the current month is -->
  220.      // set to December (NumValue = 12)
  221.      private void btnDateLeft_Click(object sender, RoutedEventArgs e)
  222.      {
  223.          _parentForm.bindings.schedule.Clear();
  224.          if (NumValue > 1 && NumValue <= 12)
  225.          {
  226.              NumValue -= 1;
  227.  
  228.              _nameofmonth = NumValue;
  229.  
  230.  
  231.          }
  232.          else
  233.          {
  234.              NumYear -= 1;
  235.              _nameofyear = NumYear;
  236.              NumValue = 12;
  237.              _nameofmonth = NumValue;
  238.  
  239.          }
  240.          makeCalender();
  241.      }
  242.  
  243.     // btnCalenderDate_Click (as of 11/5/2011) will only return the current month, day, year in messagebox
  244.     // --> Problem is displaying an event on the textblock(gridcell) of the selected button(date). -->
  245.     // Example: If I click on the button 5 of november 2011(11/5/2011) I should be able to make an event
  246.     // and display the title of the event on that pictular gridcell of 11/5/2011.
  247.      private void btnCalenderDate_Click(object sender, RoutedEventArgs e)
  248.      {
  249.          Button b = e.Source as Button;
  250.  
  251.          int day = Convert.ToInt32(b.Content);
  252.          MessageBox.Show(NumValue + "/" + b.Content + "/" + NumYear);
  253.          //b.Content = "123";
  254.  
  255.          newevent = new Event(NumValue, day, NumYear);
  256.          newevent.Show();
  257.  
  258.  
  259.  
  260.      }
  261.  
  262.     //btnCalenderDate_Loaded will determine todays date and highlight the button.
  263.      private void btnCalenderDate_Loaded(object sender, RoutedEventArgs e) //Sets the current day on the calender
  264.      {
  265.          DateTime date = DateTime.Now;
  266.          Button today = e.Source as Button;
  267.  
  268.          if (NumValue == date.Month && (String)today.Content == date.Day.ToString() && NumYear == date.Year)
  269.          {
  270.              today.Background = Brushes.Aqua;
  271.          }
  272.      }