Создание расписания

Основной код

XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Button x:Name="BtnDown" Click="BtnDown_Click" Content="<" HorizontalAlignment="Left" Width="30" Height="30" Margin="10">
        <Button.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="15"></Setter>
            </Style>
        </Button.Resources>
    </Button>
    <Button x:Name="BtnUp" Click="BtnUp_Click"  Content=">" HorizontalAlignment="Right" Width="30" Height="30" Margin="10">
        <Button.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="15"></Setter>
            </Style>
        </Button.Resources>
    </Button>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid x:Name="GridPanel" Grid.Column="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>

                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>

                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
        </Grid>
    </Grid>
</Grid>
      

C#

List<string> weeks = new List<string> { "Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс" };
DateTime dateNow;

public UserSchedule()
{
    InitializeComponent();

    dateNow = DateTime.Now;
    Refresh();
}

private void Refresh()
{
    GridPanel.Children.Clear();

    var dateStart = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day);

    for (int index_hour = 0; index_hour < 24; index_hour++)
    {
        var textH = $"{index_hour}:00";
        if (index_hour < 10)
            textH = $"0{index_hour}:00";

        var textHour = new TextBlock()
        {
            Text = textH,
        };
        GridPanel.Children.Add(textHour);
        Grid.SetRow(textHour, index_hour + 1);
    }

    for (int index_day = 0; index_day < 5; index_day++)
    {
        var dat = (int)dateStart.DayOfWeek - 1;
        if (dat == -1)
            dat = 6;

        var textDay = new TextBlock()
        {
            Text = $"{weeks[dat]} {dateStart.Day}",
            HorizontalAlignment = HorizontalAlignment.Center,
        };
        GridPanel.Children.Add(textDay);
        Grid.SetColumn(textDay, index_day + 1);

        var events = App.DB.Events.Where(x => x.DateStart.Year == dateStart.Year && x.DateStart.Month == dateStart.Month && x.DateStart.Day == dateStart.Day).ToList();
        for (int index_hour = 0; index_hour < 24; index_hour++)
        {
            var eventsHour = events.Where(x => x.DateStart.Hour == index_hour).ToList();

            if (!eventsHour.Any())
                continue;

            var button = new Button()
            {
                Content = $"{eventsHour.Count()}",
                DataContext = eventsHour,
            };
            button.Click += Button_Click;

            GridPanel.Children.Add(button);
            Grid.SetColumn(button, index_day + 1);
            Grid.SetRow(button, index_hour + 1);
        }
        dateStart = dateStart.AddDays(1);
    }
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    var events = (sender as Button).DataContext as List<Event>;

    var text = "Мероприятия:";
    foreach (var even in events)
        text += "\n" + even.Name;

    MessageBox.Show(text);
}

private void BtnDown_Click(object sender, RoutedEventArgs e)
{
    dateNow = dateNow.AddDays(-1);
    Refresh();
}

private void BtnUp_Click(object sender, RoutedEventArgs e)
{
    dateNow = dateNow.AddDays(1);
    Refresh();
}