在自定义日历wpf上显示天数

发布于 2024-12-11 20:44:45 字数 3065 浏览 1 评论 0原文

我正在尝试在自定义日历上的正确位置显示该月的天数。使用绑定和使用可观察集合,我有一个 7 列和 6 行的网格,我需要在每个文本块上显示日期(数字)。我已经让自己完全迷失了,我不知道还能尝试什么。不知何故,我需要获取星期几以及今天是星期几。我什至不确定我是否以正确的方式处理这件事。我知道 XAML 很好(至少应该是),但我对使用日期时间和可观察集合感到困惑。非常感谢任何帮助。

在这里,我使用 Observable 集合:

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

这是我拥有的 Schedule 类:

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() 的类;

I'm trying to display the days of the month on a custom calendar in the right spot. Using binding and using observable collection I have a grid with 7 columns and 6 rows, I need to display the date(number) on each textBlock. I've gotten myself to a point that I'm completely lost and I don't know what else to try. Somehow I need to get the day of week and what day it is. I'm not even sure if I'm going about this the right way. I know the XAML is good(should be at least) I'm confused on using datetime and observable collections though. Any help is greatly appreciated.

Here I use Observable collection:

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

This is a Schedule class I have:

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

And the 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>

I also have a class creating a new ObservableCollection();

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风为裳 2024-12-18 20:44:45

创建日期的循环可能应该是日期循环,其中 WeekNo 和 WeekDay 是根据日期计算的值。

例如,

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

它使用扩展方法来查找该月内的周数,可以在 关于另一个SO问题

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

Your loop for creating the dates should probably be a loop through Dates, with the WeekNo and WeekDay being values calculated based on the date

For example,

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

It uses an Extension Method to find the week number within the month, which can be found 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文