2011-10-27 2 views
0

カレンダーの日付を正しい場所に表示しようとしています。バインディングを使用して、観測可能なコレクションを使用すると、私は7列と6行のグリッドを持っているので、各textBlockに日付(数値)を表示する必要があります。私は完全に迷っていて、何を試していいのか分からないという点に気づいた。どういうわけか、私は週の日とそれが何日あるかを知る必要があります。私はこれについて正しいことをしているのかどうかもわかりません。私はXAMLが良い(少なくともでなければならない)ことを知っていますが、私はdatetimeとobservableコレクションを使用して混乱しています。どんな助けでも大歓迎です。カスタムカレンダーの日数を表示するwpf

ここで私が観察コレクションを使用します。

public SchedulePage(MainWindow parentForm) 
{ 
    InitializeComponent(); 
    _parentForm = parentForm; 
    // DateTime date = new DateTime(year, month, day); 

    _parentForm.bindings = new BindingCamper(); 

    int day = DateTime.DaysInMonth(2011, 10); 

    for (int i = 0; i < 6; i++) 
    { 
     for (int j = 0; j < 7; j++) 
     { 
     _parentForm.bindings.schedule.Add(new Schedule { WeekNo = i, WeekDay = j }); 
     DataContext = _parentForm.bindings; 
     } 
    } 
} 

は、これは私が持っているスケジュールクラスです:

public class Schedule //: INotifyPropertyChanged 
{ 
    public int WeekNo { get; set; } 
    public int WeekDay { get; set; } 
    public DateTime Date { get; set; } 

    public int datenum 
    { 
     get { return (int)Date.DayOfWeek; } 
    } 
} 

とXAML:

<ItemsControl ItemsSource="{Binding schedule}" Name="Calender" Margin="0,34,0,0" VerticalAlignment="Stretch"> 
    <ItemsControl.Template> 
    <ControlTemplate TargetType="ItemsControl" > 
     <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15"> 
      <ItemsPresenter/> 
     </Border> 

    </ControlTemplate> 



    </ItemsControl.Template> 
<!-- ItemsPanelTemplate --> 
    <ItemsControl.ItemsPanel> 

    <ItemsPanelTemplate> 

     <Grid ShowGridLines="True" Name="gridCalender"> 

      <Grid.RowDefinitions> 
       <RowDefinition /> 
       <RowDefinition /> 
       <RowDefinition /> 
       <RowDefinition /> 
       <RowDefinition /> 
       <RowDefinition /> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 

     </Grid> 
    </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 

<ItemsControl.ItemTemplate> 

    <DataTemplate> 

     <TextBlock Text ="{Binding datenum}" Background="#FFBCF4E0" OpacityMask="Black" Grid.Column="{Binding datenum}" /> 


    </DataTemplate> 
</ItemsControl.ItemTemplate> 


<!-- ItemContainerStyle --> 
<ItemsControl.ItemContainerStyle> 
    <Style > 
     <Setter Property="Grid.Column" Value="{Binding WeekDay}" /> 
     <Setter Property="Grid.Row" Value="{Binding WeekNo}" /> 

    </Style> 
</ItemsControl.ItemContainerStyle> 
</ItemsControl> 

私も作成したクラスを持っています新しいObservableCollection();

答えて

0

日付を作成するためのあなたのループは、おそらく

DateTime today = DateTime.Now; 
DateTime startDate = new DateTime(today.Year, today.Month, 1); 
DateTime endDate = startDate.AddMonths(1).AddDays(-1); 

while (startDate <= endDate) 
{ 
    DayCollection.Add(new Day 
    { 
     Date = startDate, 
     WeekDay = (int)startDate.DayOfWeek, 
     WeekNo = startDate.GetWeekOfMonth() 
    }); 

    startDate = startDate.AddDays(1); 
} 

、WeekNoと平日は例えば日付

に基づいて算出した値であることと、日付をループする必要があり、それが拡張メソッドを使用しています見つけることができ、月内の週番号を、見つけるon another SO question

static class DateTimeExtensions 
{ 
    static GregorianCalendar _gc = new GregorianCalendar(); 
    public static int GetWeekOfMonth(this DateTime time) 
    { 
     DateTime first = new DateTime(time.Year, time.Month, 1); 
     return time.GetWeekOfYear() - first.GetWeekOfYear() + 1; 
    } 

    static int GetWeekOfYear(this DateTime time) 
    { 
     return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); 
    } 
}