如何在日历WPF上切换月份

时间:2011-10-29 01:03:31

标签: c# wpf visual-studio-2010 observablecollection

这是在扼杀我。现在通过创建一个新的DateTime我只能在我拥有的日历(网格)上显示一个月。例如,如果我说DateTime(2011,10,1),它会找到weekofmonth和dayofweek,并将每个日期放在日历上的右方块上(网格,7列,6行)。

我希望能够以某种方式从列表框或组合框或其他内容中选择并选择一个月,日历将更新到该月份和年份。我有INotifyPropertyChangedObservable集合我将grid.columns绑定到工作日,grid.row绑定到weeknumber(这就是它如何找到每个日期的位置)。我实际上并不知道刷新并显示另一个月。我已经阅读了关于制作收藏品的内容,但我不确定如何做到这一点以及如何使用INotifyPropertyChanged

我已经把时间比我应该的时间多了。请任何帮助表示赞赏。

日程安排课程:

public class Schedule : INotifyPropertyChanged
{
    private int MonthofYear = 11;
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public int NameofMonth
    {
        get
        {
            return this.MonthofYear;
        }

        set
        {
            if (value != this.MonthofYear)
            {
                this.MonthofYear = value;
                NotifyPropertyChanged("NameofMonth");
            }
        }
    }

    // public void UpdateCal(PropertyChangedEventArgs e)
    // {
    //    if (PropertyChanged != null)
    //        PropertyChanged(this, e);
    // } 
    public string MonthWeek { get; set; }
    public string Year { get; set; }
    public string Month { get; set; }
    public string day { get; set; }
    public string WeekOfYear { get; set; }
    public string dayofweek { get; set; }


    public int WeekNo { get; set; }
    public int WeekDay { get; set; }
    public DateTime Date { get; set; }

这门课使ObservableCollection

public partial class BindingCamper : Schedule 
{  // This class assist in binding campers from listview to the textboxes on the camperspage
    public ObservableCollection<Camper> Campers { get; set; }
    public ObservableCollection<Staff> StaffMembers { get; set; }
    public ObservableCollection<Schedule> schedule { get; set; }
    Schedule hut = new Schedule();
    public BindingCamper()
    {
        Campers = new ObservableCollection<Camper>();
        StaffMembers = new ObservableCollection<Staff>();
        schedule = new ObservableCollection<Schedule>();
        //NotifyPropertyChanged("schedule");
    }

这是重要的课程。 DateTime newcurr = DateTime(2011, 10, 1) - 这就是我可以投入的全部内容。这会为该日期创建日历。我希望能够为多个日期创建日历(虽然一次一个)。

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

public partial class SchedulePage : Page
{
    public int pick2;

    MainWindow _parentForm;
    public int pick;
    Schedule sched = new Schedule();


    private ObservableCollection<Schedule> schedules;
    public ObservableCollection<Schedule> Sched
    {
        get { return schedules; }
        set
        {
            if (Equals(schedules, value)) return;

            schedules = value;
            sched.NotifyPropertyChanged("Sched"); 
        }
    }

    public SchedulePage(MainWindow parentForm)
    {
        InitializeComponent();

        sched.NameofMonth = comboMonth.SelectedIndex;
        pick = Convert.ToInt32(comboMonth.SelectedItem);
        _parentForm = parentForm;
        // DateTime date = new DateTime(year, month, day);
        int ugly = sched.NameofMonth;
        AcceptDate(2011, 12);
    }

    public void AcceptDate(int year, int acceptmonth) {
        var t = new List<Schedule>();
        DateTime curr = DateTime.Now;
        int y = acceptmonth;
        //  comboMonth.Items.Add(curr.Month);
        DateTime newcurr = new DateTime(2011, 12, 1);
        //   pickdate = datePickercal.SelectedDate;
        //  DateTime newcurr = new DateTime(curr.Year, curr.Month, 1);
        var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
        var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
        for (var i = 1; newcurr.Month == acceptmonth; newcurr = newcurr.AddDays(1))
        {

            var month_week = (newcurr.Day / 7);
            sched.MonthWeek = newcurr.GetWeekOfMonth().ToString();
            sched.Month = newcurr.Month.ToString();
            sched.Year = newcurr.Year.ToString();
            sched.day = newcurr.Day.ToString();
            sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
            sched.dayofweek = newcurr.DayOfWeek.ToString();
            t.Add(sched);

            _parentForm.bindings.schedule.Add(new Schedule { WeekNo = newcurr.GetWeekOfMonth() - 1, WeekDay = (int)newcurr.DayOfWeek, day = newcurr.Day.ToString() });

        }
        lblDate.Content = (newcurr.Month - 1) + "/" + newcurr.Year;
        //testGrid.ItemsSource = t;

        DataContext = _parentForm.bindings;

    }

    private void btnDateRight_Click(object sender, RoutedEventArgs e)
    { 

    }

    private void btnDateLeft_Click(object sender, RoutedEventArgs e)
    {

    }

}

对于XAML Itemsource = schedule

<Grid>
    <ItemsControl ItemsSource="{Binding schedule}"  Name="Calender" Margin="0,49,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 OpacityMask="Black"  Name="txtBlockdays">
                    <Button Content="{Binding day}" Width="175" HorizontalAlignment="Stretch" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Background="#FF94EBEB">

                    </Button> 
                </TextBlock>
            </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>
    <Grid Margin="0,0,0,262">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Label Content="Sunday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,12,0" Name="label1" VerticalAlignment="Top" Width="Auto" />
        <Label Content="Monday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label2" VerticalAlignment="Top" Grid.Column="1" />
        <Label Content="Tuesday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label3" VerticalAlignment="Top" Grid.Column="2" />
        <Label Content="Wednesday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label4" VerticalAlignment="Top" Grid.Column="3" />
        <Label Content="Thursday" Height="28" HorizontalAlignment="Stretch" Name="label5" VerticalAlignment="Top" Grid.Column="4" Margin="0,25,0,0" />
        <Label Content="Friday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label6" VerticalAlignment="Top" Grid.Column="5" />
        <Label Content="Saturday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label7" VerticalAlignment="Top" Grid.Column="6" />
        <Label Height="28" HorizontalAlignment="Left" Margin="63,0,0,0" Name="lblDate" VerticalAlignment="Top" Grid.Column="2" Width="127" Grid.ColumnSpan="3" />
        <Button Content="&lt;" Height="23" HorizontalAlignment="Left" Margin="28,6,0,0" Name="btnDateLeft" VerticalAlignment="Top" Width="29" Grid.Column="2" Command="NavigationCommands.BrowseBack" />
        <Button Content="&gt;" Height="23" HorizontalAlignment="Left" Margin="20,5,0,0" Name="btnDateRight" VerticalAlignment="Top" Width="30" Grid.Column="4" Command="NavigationCommands.BrowseForward" />
        <ComboBox Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="6,0,0,0" Name="comboMonth" VerticalAlignment="Top" Width="120">
            <ComboBoxItem Content="{Binding Month}" IsSelected="False" />
            <ComboBoxItem Content="2" />
            <ComboBoxItem Content="3" />
            <ComboBoxItem Content="4" />
            <ComboBoxItem Content="5" />
            <ComboBoxItem Content="6" />
            <ComboBoxItem Content="7" />
            <ComboBoxItem Content="8" />
            <ComboBoxItem Content="9" />
            <ComboBoxItem Content="10" />
            <ComboBoxItem Content="11" IsSelected="False" />
            <ComboBoxItem Content="12" />
        </ComboBox>
        <ListBox Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="48,0,0,0" Name="listMe" VerticalAlignment="Top" Width="120" Grid.Column="5">
            <ListBoxItem Content="01" IsSelected="False" />
            <ListBoxItem Content="3" IsSelected="True" />
            <ListBoxItem Content="2" />
        </ListBox>
    </Grid>
</Grid>

0 个答案:

没有答案