2011-10-27 2 views
0

나는 올바른 자리에 맞춤 캘린더에 달의 요일을 표시하려고합니다. 바인딩을 사용하고 관찰 가능한 콜렉션을 사용하면 7 열 6 행의 그리드가 있고 각 textBlock에 날짜 (숫자)를 표시해야합니다. 나는 완전히 잃어 버렸고 나는 그 밖의 무엇을 시도해야할지 모른다. 어쨌든 나는 요일과 그 날을 알아야합니다. 나는 이것이 올바른 방향으로 가고 있는지 확신 할 수 없다. 나는 XAML이 좋다는 것을 알고있다. 적어도 datetime과 observable 컬렉션을 혼란스러워한다. 어떤 도움이라도 대단히 감사합니다. 여기맞춤 캘린더에서 일 표시 wpf

내가 관찰 가능한 모음을 사용 :

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> 

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; 
     } 
    } 
} 

이것은 일정 내가 가진 클래스 새로운 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); 
    } 
}