您好我需要能够生成带有文本的动态矩形。我现在遇到了问题,我无法在矩形上添加文字
我在这里生成矩形:
Sub FindFirstEmptyValue()
Dim lastRow As Long
With Worksheets("Sheet1")
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
.Cells(lastRow, 2) = .Range("B3").value
.Cells(lastRow, 3) = .Range("C3").value
End With
End Sub
在研究其他类似问题时,答案总是避免使用矩形,但我希望继续使用矩形。
答案 0 :(得分:1)
您可以将TextBlock子项添加到Grid,与Rectangle位于同一位置。
您还可以创建一个包含两个子节点的子网格,Rectangle和TextBlock,如下所示:
Grid subGrid = new Grid();
subGrid.Children.Add(rectangle);
TextBlock textblock = new TextBlock();
textblock.Text = "Text to add";
subGrid.Children.Add(textblock);
Kalender.Children.Add(grid);
或者将TextBlock添加为Border的子级,而不是使用Rectangle:
var border = new Border
{
Background = termin.color,
Child = new TextBlock { Text = "Some Text" }
};
Grid.SetRow(border, termin.start + 2);
Grid.SetColumn(border, termin.day * 2 - 1);
Grid.SetColumnSpan(border, 2);
Grid.SetRowSpan(border, termin.end - termin.start);
Kalender.Children.Add(border);
或使用适当对齐的标签:
var label = new Label
{
Content = "Some Text",
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Background = termin.color
};
Grid.SetRow(label, termin.start + 2);
Grid.SetColumn(label, termin.day * 2 - 1);
Grid.SetColumnSpan(label, 2);
Grid.SetRowSpan(label, termin.end - termin.start);
Kalender.Children.Add(label);
答案 1 :(得分:-1)
public void ShowAppointements()
{
foreach (Termin termin in MainWindow.termine)
{
if (termin.week == Int32.Parse(txtWeek.Content.ToString()))
{
Rectangle rectangle = new Rectangle();
TextBlock textblock = new TextBlock();
Kalender.Children.Add(rectangle);
Kalender.Children.Add(textblock);
Grid.SetRow(rectangle, termin.start + 2);
Grid.SetColumn(rectangle, termin.day * 2 - 1);
Grid.SetColumnSpan(rectangle, 2);
Grid.SetRowSpan(rectangle, termin.end - termin.start);
Grid.SetRow(textblock, termin.start + 2);
Grid.SetColumn(textblock, termin.day * 2 - 1);
Grid.SetColumnSpan(textblock, 2);
Grid.SetRowSpan(textblock, termin.end - termin.start);
textblock.Text = termin.project + "\n" + termin.employee;
rectangle.Fill = termin.color;
}
}
}