如何在日历 WPF 上切换月份

发布于 2024-12-12 17:24:26 字数 11415 浏览 1 评论 0原文

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

我希望能够以某种方式从列表框或组合框或其他内容中进行选择,然后选择一个月,日历将更新到该月份和年份。我有 INotifyPropertyChanged 和 Observable 集合,我将 grid.columns 绑定到工作日,将 grid.row 绑定到 weeknumber (这就是它找到放置每个日期的位置的方式)。我实际上不知道刷新并显示另一个月。我读过有关创建集合的内容,但我不确定如何完成以及如何与 INotifyPropertyChanged 配合使用。

我在这方面投入的时间比我应该投入的时间多得多。请提供任何帮助。

Schedule类:

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>

This is killing me. Right now by creating a new DateTime I can only display one month on the calendar (grid) I have. For example, if I say DateTime(2011,10,1), it'll find weekofmonth and dayofweek and place each date on the right square on the calendar (grid, 7 columns, 6 rows).

I want to be able to somehow pick from a listbox or combobox or something and pick a month and the calendar will update to that month and year. I have INotifyPropertyChanged and Observable collection I'm binding the grid.columns to weekday and grid.row to weeknumber (that's how it finds where to place each date). I don't actually know to refresh and show another month. I've read things about making a collection but I'm not sure how that would be done and how that works with INotifyPropertyChanged.

I have put way more time than I should have in this. Please any help is appreciated.

Schedule class:

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

This class makes 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");
    }

This is the important class. DateTime newcurr = DateTime(2011, 10, 1) - this is all I can put in. This creates a calendar for that date. I want to be able to create calendars for multiple dates (one at a time though).

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)
    {

    }

}

And for 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="<" Height="23" HorizontalAlignment="Left" Margin="28,6,0,0" Name="btnDateLeft" VerticalAlignment="Top" Width="29" Grid.Column="2" Command="NavigationCommands.BrowseBack" />
        <Button Content=">" 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文