XAML WPF使用循环更改多个文本块的文本

hmtdttj4  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(162)

我正在尝试制作自己的日历,并希望根据月份初始化日历中的日期。
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. 
            }

谢谢你的帮助。我真的被困在这里了。如果你知道一个更好的解决办法,我会很乐意的。

sz81bmfz

sz81bmfz1#

我做了类似的事情在过去,我进行如下:

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

请告诉我这是否如你所愿

0md85ypi

0md85ypi2#

谢谢你的帮助Filippo Ferrario。我用你说的做了这个。

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

这是我的日历的完整代码,如果有人需要它。

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://learn.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]);
      } 
}

相关问题