I am writing a WPF application. I have a Calendar and Buttons for previous (<), current (today) and next (>) month. My problem is, when I switch a month for next or previous, first day of each month starts always on the same day.
I have a panel where I create other panels. Each panel represents one day. My code should put a label with the number of day in a correct position, but it always starts from the first WrapPanel. Where is the problem? Some screenshots below.
After clicking next Month Button, February should start in Thursday.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ActivityMonitor
{
/// <summary>
/// Logika interakcji dla klasy CalendarWindow.xaml
/// </summary>
public partial class CalendarWindow : Window
{
//lista dni w danym miesiacu
private List<WrapPanel> daysList = new List<WrapPanel>();
//aktualna data
private DateTime currentDate = DateTime.Today;
public CalendarWindow()
{
InitializeComponent();
DisplayCalendar();
}
//metoda wyświetlająca kalendarz
private void DisplayCalendar()
{
GenerateDayPanel(42);
//AddDayLabelToWrap(GetFirstDayOfCurrentDate(), GetTotalDaysOfCurrentDate());
DisplayCurrentDate();
}
//metoda
private int GetFirstDayOfCurrentDate()
{
DateTime firstDayOfMonth = new DateTime(currentDate.Year, currentDate.Month, 1);
return (int) firstDayOfMonth.DayOfWeek + 1;
}
private int GetTotalDaysOfCurrentDate()
{
DateTime firstDayOfCurrentDate = new DateTime(currentDate.Year, currentDate.Month, 1);
return firstDayOfCurrentDate.AddMonths(1).AddDays(-1).Day;
}
private void DisplayCurrentDate()
{
labelMonthAndYear.Content = currentDate.ToString("MMMM, yyyy");
AddDayLabelToWrap(GetFirstDayOfCurrentDate(), GetTotalDaysOfCurrentDate());
}
//metoda ustawuająca miesiąc na poprzedni
private void PreviousMonth()
{
currentDate = currentDate.AddMonths(-1);
DisplayCurrentDate();
}
//metoda ustawiająca miesiąc na następny
private void NextMonth()
{
currentDate = currentDate.AddMonths(1);
DisplayCurrentDate();
}
//metoda ustawiająca miesiąc na aktualny
private void Today()
{
currentDate = DateTime.Today;
DisplayCurrentDate();
}
//metoda generująca dni tygodnia danego miesiąca
private void GenerateDayPanel(int totalDays)
{
daysPanel.Children.Clear();
daysList.Clear();
for (int i = 1; i <= totalDays; i++)
{
var wrap = new WrapPanel();
wrap.Name = $"wrap{i}";
wrap.ItemWidth = 200;
wrap.ItemHeight = 100;
if(i%2 == 0)
{
wrap.Background = new SolidColorBrush(Colors.LightBlue);
}
else
{
wrap.Background = new SolidColorBrush(Colors.Gray);
}
daysPanel.Children.Add(wrap);
daysList.Add(wrap);
}
}
//metoda dodająca labele z numerami dni miesiąca
private void AddDayLabelToWrap(int startDayAtPanel, int totalDaysInMonth)
{
foreach (WrapPanel wrap in daysList)
{
wrap.Children.Clear();
}
for (int i = 1; i <= totalDaysInMonth; i++)
{
var lab = new Label();
lab.Name = $"lblDay{i}";
lab.Content = i;
lab.HorizontalContentAlignment = HorizontalAlignment.Right;
daysList[(i - 1) + (startDayAtPanel - 1)].Children.Add(lab);
}
}
private void ButtonPrevMonth_Click(object sender, RoutedEventArgs e)
{
PreviousMonth();
}
private void ButtonNextMonth_Click(object sender, RoutedEventArgs e)
{
NextMonth();
}
private void ButtonToday_Click(object sender, RoutedEventArgs e)
{
Today();
}
}
}
I made the sample you need and put it on the GitHub.
This is not as simple as I thought. And like other people's advice, writing the UI in the behind code is not a good way in the long run, so I hope you study this sample source code that I made for you!
Just point!
Using ListBox
Style & Template
MainWindow (.xaml)
Code Behind (.cs)
Model