Home > Software design >  WPF Change the text of mulitplie textblocks using a loop
WPF Change the text of mulitplie textblocks using a loop

Time:08-13

I'm trying to make my own calendar and want to initialize the days in the calendar based on the month.

XAML:

<TextBlock x:Name="day0" Grid.Column="0" Grid.Row="0" Text="0" Style="{StaticResource DayTextBlock}"/>

C#:

List<UIElement> controls = new List<UIElement>();
            controls.Add(day0);
            controls.Add(day1);
            controls.Add(day2);
            controls.Add(day3);
            controls.Add(day4);
            controls.Add(day5);
            controls.Add(day6);

            for (int i = 0; i < daysInMonth; i  )
            {
                day0.Text = i.ToString(); // I can change it this way but don't want to do this for all 30 days
                controls[i].Text = i.ToString(); // This is more so what I want to do. 
            }

Thanks for any help. I'm really stuck here. If you know of a better way of going about this I'd be very open to it.

CodePudding user response:

I did a similar thing in the past and I proceeded as following:

TextBlock CreateDay(int dayNumber)
{
    return new TextBlock()
    {
        Text = dayNumber.ToString()
        // Here you set you other default properties, common to all TextBlocks
    };
}

TextBlock day;
for (int i = 1; i < daysInMonth; i  )
{
    int position = i - 1;
    day = CreateDay(i);
    
    // The '7' is the number of columns that your grid has
    Grid.SetRow(day, position / 7);
    Grid.SetColumn(day, position % 7);

    yourGridName.Children.Add(day);
}

Please, let me know if this worked as you wanted

CodePudding user response:

Thank you for the help Filippo Ferrario. I used what you said and made this.

List<Button> buttonList = new List<Button>();
List<TextBlock> textList = new List<TextBlock>();
for (int i = 0; i < daysInMonth; i  )
{
     buttonList.Add(new Button());
     textList.Add(new TextBlock());
     textList[i].Text = "\t"   i;
}

for (int i = 0; i < daysInMonth; i  )
{
     textList[i].Style = textStyle;
     buttonList[i].Style = buttonStyle;
     if (column > 6)
     {
          column = 0;
          row  ;
     }
     Grid.SetColumn(textList[i], column);
     Grid.SetColumn(buttonList[i], column);
     column  ;
     Grid.SetRow(textList[i], row);
     Grid.SetRow(buttonList[i], row);
     calNum.Children.Add(buttonList[i]);
     calNum.Children.Add(textList[i]);
} 

This is the full code for my calendar if anyone needs it.

private void displayDays()
{
     DateTime dateTime = DateTime.Now;
     Month.Text = dateTime.ToString("MMMM"); //month title
   
     DateTime startOfMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
     int daysInMonth = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);
     int dayOfWeek = Convert.ToInt32(startOfMonth.DayOfWeek.ToString("d"))   1; //https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=net-6.0#system-datetime

     List<Button> buttonList = new List<Button>();
     List<TextBlock> textList = new List<TextBlock>();
     for (int i = 0; i < daysInMonth; i  )
     {
          buttonList.Add(new Button());
          textList.Add(new TextBlock());
          textList[i].Text = "\t"   i;
      }

      //calNum is a grid I made in the XAML
      calNum.ColumnDefinitions.Add(new ColumnDefinition());
      calNum.ColumnDefinitions.Add(new ColumnDefinition());
      calNum.ColumnDefinitions.Add(new ColumnDefinition());
      calNum.ColumnDefinitions.Add(new ColumnDefinition());
      calNum.ColumnDefinitions.Add(new ColumnDefinition());
      calNum.RowDefinitions.Add(new RowDefinition());
      calNum.RowDefinitions.Add(new RowDefinition());
      calNum.RowDefinitions.Add(new RowDefinition());
      calNum.RowDefinitions.Add(new RowDefinition());
      calNum.RowDefinitions.Add(new RowDefinition());

      int row = 0;
      int column = 6;
      column  = dayOfWeek;
      Style textStyle = this.FindResource("DayTextBlock") as Style;
      Style buttonStyle = this.FindResource("DayButton") as Style;

      for (int i = 0; i < daysInMonth; i  )
      {
           textList[i].Style = textStyle;
           buttonList[i].Style = buttonStyle;
           if (column > 6)
           {
                column = 0;
                row  ;
           }
           Grid.SetColumn(textList[i], column);
           Grid.SetColumn(buttonList[i], column);
           column  ;
           Grid.SetRow(textList[i], row);
           Grid.SetRow(buttonList[i], row);
           calNum.Children.Add(buttonList[i]);
           calNum.Children.Add(textList[i]);
      } 
}
  • Related