locked
Need some advice on my self-making CALENDAR (WPF xaml C#) RRS feed

  • Question

  • Hello! It has been 6 months since I started learning WPF & xaml.  I am a beginner. Tons of things I have to study...
    So, your advice is really appreciated...

    Anyways, here's my codes (xaml & cs) ...

    xaml file

    <Window x:Class="WPFCalendarAlpha.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WPFCalendarAlpha"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="450" Loaded="Window_Loaded">
        <Window.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="TextAlignment" Value="Center"/>
                <Setter Property="FontSize" Value="20"/>
                <Setter Property="Margin" Value="2"/>
            </Style>
        </Window.Resources>
        <Grid>
            <Grid x:Name="GridCalendar" Grid.Row="1" Grid.Column="0" Grid.RowSpan="1" Margin="10,10,10,10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
    
                <Button x:Name="BtnPrev" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Width="80" Height="40" Content="PREV MONTH" FontSize="10" Background="White"  Click="BtnPrev_Click"/>
                <TextBlock x:Name="txblkMonth" Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="3" Text="XXXX/MM" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <Button x:Name="BtnNext" Grid.Column="5" Grid.Row="0" Grid.ColumnSpan="2" Width="80" Height="40" Content="NEXT MONTH" FontSize="10" Background="White" Click="BtnNext_Click"/>
    
                <TextBlock  x:Name = "su"  Grid.Column="0" Grid.Row="1" Text="Sun" Foreground="OrangeRed"/>
                <TextBlock  x:Name = "mo"  Grid.Column="1" Grid.Row="1" Text="Mon"/>
                <TextBlock  x:Name = "tu"  Grid.Column="2" Grid.Row="1" Text="Tue"/>
                <TextBlock  x:Name = "we"  Grid.Column="3" Grid.Row="1" Text="Wed"/>
                <TextBlock  x:Name = "th"  Grid.Column="4" Grid.Row="1" Text="Thu"/>
                <TextBlock  x:Name = "fr"  Grid.Column="5" Grid.Row="1" Text="Fri"/>
                <TextBlock  x:Name = "sa"  Grid.Column="6" Grid.Row="1" Text="Sat" Foreground="DodgerBlue"/>
                <TextBlock  x:Name = "d0"  Grid.Column="0" Grid.Row="2" Foreground="OrangeRed"/>
                <TextBlock  x:Name = "d1"  Grid.Column="1" Grid.Row="2" />
                <TextBlock  x:Name = "d2"  Grid.Column="2" Grid.Row="2" />
                <TextBlock  x:Name = "d3"  Grid.Column="3" Grid.Row="2" />
                <TextBlock  x:Name = "d4"  Grid.Column="4" Grid.Row="2" />
                <TextBlock  x:Name = "d5"  Grid.Column="5" Grid.Row="2" />
                <TextBlock  x:Name = "d6"  Grid.Column="6" Grid.Row="2" Foreground="DodgerBlue"/>
                <TextBlock  x:Name = "d7"  Grid.Column="0" Grid.Row="3" Foreground="OrangeRed"/>
                <TextBlock  x:Name = "d8"  Grid.Column="1" Grid.Row="3" />
                <TextBlock  x:Name = "d9"  Grid.Column="2" Grid.Row="3" />
                <TextBlock  x:Name = "d10" Grid.Column="3" Grid.Row="3" />
                <TextBlock  x:Name = "d11" Grid.Column="4" Grid.Row="3" />
                <TextBlock  x:Name = "d12" Grid.Column="5" Grid.Row="3" />
                <TextBlock  x:Name = "d13" Grid.Column="6" Grid.Row="3" Foreground="DodgerBlue"/>
                <TextBlock  x:Name = "d14" Grid.Column="0" Grid.Row="4" Foreground="OrangeRed"/>
                <TextBlock  x:Name = "d15" Grid.Column="1" Grid.Row="4" />
                <TextBlock  x:Name = "d16" Grid.Column="2" Grid.Row="4" />
                <TextBlock  x:Name = "d17" Grid.Column="3" Grid.Row="4" />
                <TextBlock  x:Name = "d18" Grid.Column="4" Grid.Row="4" />
                <TextBlock  x:Name = "d19" Grid.Column="5" Grid.Row="4" />
                <TextBlock  x:Name = "d20" Grid.Column="6" Grid.Row="4" Foreground="DodgerBlue"/>
                <TextBlock  x:Name = "d21" Grid.Column="0" Grid.Row="5" Foreground="OrangeRed"/>
                <TextBlock  x:Name = "d22" Grid.Column="1" Grid.Row="5" />
                <TextBlock  x:Name = "d23" Grid.Column="2" Grid.Row="5" />
                <TextBlock  x:Name = "d24" Grid.Column="3" Grid.Row="5" />
                <TextBlock  x:Name = "d25" Grid.Column="4" Grid.Row="5" />
                <TextBlock  x:Name = "d26" Grid.Column="5" Grid.Row="5" />
                <TextBlock  x:Name = "d27" Grid.Column="6" Grid.Row="5" Foreground="DodgerBlue"/>
                <TextBlock  x:Name = "d28" Grid.Column="0" Grid.Row="6" Foreground="OrangeRed"/>
                <TextBlock  x:Name = "d29" Grid.Column="1" Grid.Row="6" />
                <TextBlock  x:Name = "d30" Grid.Column="2" Grid.Row="6" />
                <TextBlock  x:Name = "d31" Grid.Column="3" Grid.Row="6" />
                <TextBlock  x:Name = "d32" Grid.Column="4" Grid.Row="6" />
                <TextBlock  x:Name = "d33" Grid.Column="5" Grid.Row="6" />
                <TextBlock  x:Name = "d34" Grid.Column="6" Grid.Row="6" Foreground="DodgerBlue"/>
                <TextBlock  x:Name = "d35" Grid.Column="0" Grid.Row="7" Foreground="OrangeRed"/>
                <TextBlock  x:Name = "d36" Grid.Column="1" Grid.Row="7" />
            </Grid>
        </Grid>
    </Window>
    


    cs file

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    
    namespace WPFCalendarAlpha
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            // 今日の日付 Date of Today
            readonly DateTime dateOfToday = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
    
            // 月初めの曜日 Day of week of 1st day of the month
            readonly DateTime firstDayOfWeek = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
    
            // TextBlock配列 Array of TextBlock
            public TextBlock[] txblk = new TextBlock[37];
    
            private void Window_Loaded(object sender, RoutedEventArgs e) // Window_Loaded Event Property
            {
    
                txblkMonth.Text = dateOfToday.ToString("yyyy / M");
    
                txblk[0] = d0; txblk[10] = d10; txblk[20] = d20; txblk[30] = d30;
                txblk[1] = d1; txblk[11] = d11; txblk[21] = d21; txblk[31] = d31;
                txblk[2] = d2; txblk[12] = d12; txblk[22] = d22; txblk[32] = d32;
                txblk[3] = d3; txblk[13] = d13; txblk[23] = d23; txblk[33] = d33;
                txblk[4] = d4; txblk[14] = d14; txblk[24] = d24; txblk[34] = d34;
                txblk[5] = d5; txblk[15] = d15; txblk[25] = d25; txblk[35] = d35;
                txblk[6] = d6; txblk[16] = d16; txblk[26] = d26; txblk[36] = d36;
                txblk[7] = d7; txblk[17] = d17; txblk[27] = d27;
                txblk[8] = d8; txblk[18] = d18; txblk[28] = d28;
                txblk[9] = d9; txblk[19] = d19; txblk[29] = d29;
    
                CreateCalendar(firstDayOfWeek);
            }
    
            private void CreateCalendar(DateTime date)
            {
                //カレンダーの日付をクリアにする Clear of Days of Calendar
                for (int i = 0; i < 37; i++)
                {
                    txblk[i].Text = "";
                }
                //月初めの曜日からカレンダーを作る Check day of week and create calendar
                switch (date.DayOfWeek)
                {
                    case DayOfWeek.Sunday:
                        for (int d = 0; d < DateTime.DaysInMonth(date.Year, date.Month); d++)
                        {
                            txblk[d].Text = (d + 1).ToString();
                        }
                        break;
                    case DayOfWeek.Monday:
                        for (int d = 1; d < DateTime.DaysInMonth(date.Year, date.Month) + 1; d++)
                        {
                            txblk[d].Text = d.ToString();
                        }
                        break;
                    case DayOfWeek.Tuesday:
                        for (int d = 2; d < DateTime.DaysInMonth(date.Year, date.Month) + 2; d++)
                        {
                            txblk[d].Text = (d - 1).ToString();
                        }
                        break;
                    case DayOfWeek.Wednesday:
                        for (int d = 3; d < DateTime.DaysInMonth(date.Year, date.Month) + 3; d++)
                        {
                            txblk[d].Text = (d - 2).ToString();
                        }
                        break;
                    case DayOfWeek.Thursday:
                        for (int d = 4; d < DateTime.DaysInMonth(date.Year, date.Month) + 4; d++)
                        {
                            txblk[d].Text = (d - 3).ToString();
                        }
                        break;
                    case DayOfWeek.Friday:
                        for (int d = 5; d < DateTime.DaysInMonth(date.Year, date.Month) + 5; d++)
                        {
                            txblk[d].Text = (d - 4).ToString();
                        }
                        break;
                    case DayOfWeek.Saturday:
                        for (int d = 6; d < DateTime.DaysInMonth(date.Year, date.Month) + 6; d++)
                        {
                            txblk[d].Text = (d - 5).ToString();
                        }
                        break;
                }
            }
    
            private void BtnPrev_Click(object sender, RoutedEventArgs e)
            {
                //表示月から1ヶ月引く substract a month from indicated month on the txblkMonth
                DateTime prevMonth = DateTime.ParseExact(txblkMonth.Text, "yyyy / M", null).AddMonths(-1);
    
                txblkMonth.Text = prevMonth.ToString("yyyy / M");
                CreateCalendar(prevMonth);
            }
    
            private void BtnNext_Click(object sender, RoutedEventArgs e)
            {
                //表示月に1ヶ月足す add a month to indicated month on the txblkMonth
                DateTime NextMonth = DateTime.ParseExact(txblkMonth.Text, "yyyy / M", null).AddMonths(+1);
    
                txblkMonth.Text = NextMonth.ToString("yyyy / M");
                CreateCalendar(NextMonth);
            }
        }
    }

    Calendar works as I wished at this point...

    ------------------------------------------------------------------------

    My question is as follows...

    1.
    Is it correct to use TextBlock as "Day" of calendar...? Shuold I use Label or any other UI controls?  Prospective functions are Clikck or FocusGot, MouseEnter, MouseLeave,  TouchDown, TouchUp, change color of day by some conditions, and sending date time data to other elements etc.

    2. 
    I would like to know how to bind TextBlock between .xaml file and .cs file

                <TextBlock  x:Name = "d2"  Grid.Column="2" Grid.Row="2" Text="{Bindign Path=......}" />
                <TextBlock  x:Name = "d3"  Grid.Column="3" Grid.Row="2" Text="{Bindign Path=......}" />

    and how to code in .cs...

    3. I really appreciate your advice any other things

    Thank you...



    • Moved by CoolDadTx Wednesday, July 29, 2020 2:27 PM WPF related
    Wednesday, July 29, 2020 12:50 PM

All replies