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

Untitled

By: a guest on May 26th, 2012  |  syntax: None  |  size: 3.69 KB  |  hits: 16  |  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. Displaying days on a custom calender wpf
  2. public SchedulePage(MainWindow parentForm)
  3.           {
  4.         InitializeComponent();
  5.         _parentForm = parentForm;
  6.         // DateTime date = new DateTime(year, month, day);
  7.  
  8.         _parentForm.bindings = new BindingCamper();
  9.  
  10.         int day = DateTime.DaysInMonth(2011, 10);
  11.  
  12.         for (int i = 0; i < 6; i++)
  13.             for (int j = 0; j < 7; j++)
  14.  
  15.  
  16.                     {
  17.                         {
  18.                             _parentForm.bindings.schedule.Add(new Schedule { WeekNo = i, WeekDay = j });
  19.                             DataContext = _parentForm.bindings;
  20.                         }
  21.                     }
  22.        
  23. public class Schedule //: INotifyPropertyChanged
  24. {
  25.  
  26.     public int WeekNo { get; set; }
  27.     public int WeekDay { get; set; }
  28.     public DateTime Date { get; set; }
  29.  
  30.     public int datenum
  31.     {
  32.         get { return (int)Date.DayOfWeek; }
  33.     }
  34.        
  35. <ItemsControl ItemsSource="{Binding schedule}" Name="Calender" Margin="0,34,0,0" VerticalAlignment="Stretch">
  36.         <ItemsControl.Template>
  37.             <ControlTemplate TargetType="ItemsControl" >
  38.                 <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
  39.                     <ItemsPresenter/>
  40.                 </Border>
  41.  
  42.             </ControlTemplate>
  43.  
  44.  
  45.  
  46.         </ItemsControl.Template>
  47.         <!-- ItemsPanelTemplate -->
  48.         <ItemsControl.ItemsPanel>
  49.  
  50.             <ItemsPanelTemplate>
  51.  
  52.                 <Grid ShowGridLines="True" Name="gridCalender">
  53.  
  54.                     <Grid.RowDefinitions>
  55.                         <RowDefinition />
  56.                         <RowDefinition />
  57.                         <RowDefinition />
  58.                         <RowDefinition />
  59.                         <RowDefinition />
  60.                         <RowDefinition />
  61.                     </Grid.RowDefinitions>
  62.                     <Grid.ColumnDefinitions>
  63.                         <ColumnDefinition />
  64.                         <ColumnDefinition />
  65.                         <ColumnDefinition />
  66.                         <ColumnDefinition />
  67.                         <ColumnDefinition />
  68.                         <ColumnDefinition />
  69.                         <ColumnDefinition />
  70.                     </Grid.ColumnDefinitions>
  71.  
  72.                 </Grid>
  73.             </ItemsPanelTemplate>
  74.         </ItemsControl.ItemsPanel>
  75.  
  76.         <ItemsControl.ItemTemplate>
  77.  
  78.             <DataTemplate>
  79.  
  80.                 <TextBlock Text ="{Binding datenum}" Background="#FFBCF4E0" OpacityMask="Black" Grid.Column="{Binding datenum}" />
  81.  
  82.  
  83.             </DataTemplate>
  84.         </ItemsControl.ItemTemplate>
  85.  
  86.  
  87.         <!-- ItemContainerStyle -->
  88.         <ItemsControl.ItemContainerStyle>
  89.             <Style >
  90.                 <Setter Property="Grid.Column" Value="{Binding WeekDay}" />
  91.                 <Setter Property="Grid.Row" Value="{Binding WeekNo}" />
  92.  
  93.             </Style>
  94.         </ItemsControl.ItemContainerStyle>
  95.  
  96.  
  97.  
  98.  
  99.     </ItemsControl>
  100.        
  101. DateTime today = DateTime.Now;
  102. DateTime startDate = new DateTime(today.Year, today.Month, 1);
  103. DateTime endDate = startDate.AddMonths(1).AddDays(-1);
  104.  
  105. while (startDate <= endDate)
  106. {
  107.     DayCollection.Add(new Day
  108.     {
  109.         Date = startDate,
  110.         WeekDay = (int)startDate.DayOfWeek,
  111.         WeekNo = startDate.GetWeekOfMonth()
  112.     });
  113.  
  114.     startDate = startDate.AddDays(1);
  115. }
  116.        
  117. static class DateTimeExtensions
  118. {
  119.     static GregorianCalendar _gc = new GregorianCalendar();
  120.     public static int GetWeekOfMonth(this DateTime time)
  121.     {
  122.         DateTime first = new DateTime(time.Year, time.Month, 1);
  123.         return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
  124.     }
  125.  
  126.     static int GetWeekOfYear(this DateTime time)
  127.     {
  128.         return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
  129.     }
  130. }