矩形不会显示在以编程方式编码的界面中

本文关键字:编码 方式 界面 编程 显示 | 更新日期: 2023-09-27 18:31:14

我正在通过代码构建一个接口,我需要在TextBlocks下添加两个TextBlocks和一个Rectangle作为背景。使用我的代码,TextBlocks显示,但Rectangle没有。我不知道问题出在哪里。你能帮忙吗?

TextBlock question_textblock = new TextBlock();
question_textblock.Text = question;
question_textblock.Margin = new Thickness(10, 103, 10, 0);
question_textblock.FontWeight = FontWeights.Bold;
list_grid.Children.Add(question_textblock);
TextBlock answer_textblock = new TextBlock();
answer_textblock.Text = question;
answer_textblock.Margin = new Thickness(10, 124, 10, 0);
list_grid.Children.Add(answer_textblock);
Rectangle rect = new Rectangle();
rect.Height = 69;
rect.Fill = new SolidColorBrush(Color.FromArgb(0, 0, 0, 1));
rect.Margin = new Thickness(0, 95, 0, 0);
rect.VerticalAlignment = VerticalAlignment.Top;
list_grid.Children.Add(rect);
list_grid.UpdateLayout();

矩形不会显示在以编程方式编码的界面中

解决方法:将Rectangle作为第一个孩子添加到您的list_grid中,如下所示,如果仍然没有显示,请选择不同的颜色来填充Rectangle。这应该是您想要的:

Rectangle rect = new Rectangle();
rect.Height = 69;
rect.Fill = new SolidColorBrush(Colors.Gray);
rect.Margin = new Thickness(0, 95, 0, 0);
rect.VerticalAlignment = VerticalAlignment.Top;
list_grid.Children.Add(rect);
TextBlock question_textblock = new TextBlock();
question_textblock.Text = question;
question_textblock.Margin = new Thickness(10, 103, 10, 0);
question_textblock.FontWeight = FontWeights.Bold;
list_grid.Children.Add(question_textblock);
TextBlock answer_textblock = new TextBlock();
answer_textblock.Text = question;
answer_textblock.Margin = new Thickness(10, 124, 10, 0);
list_grid.Children.Add(answer_textblock);
list_grid.UpdateLayout();