如何在 C# 中将文本添加到矩形
How can i add text to a rectangle in c#
您好,我需要能够生成带有文本的动态矩形。我现在遇到了问题,我无法在矩形
上添加文本
我在这里生成矩形:
public void ShowAppointements()
{
foreach (Termin termin in MainWindow.termine)
{
if (termin.week == Int32.Parse(txtWeek.Content.ToString()))
{
Rectangle rectangle = new Rectangle();
Kalender.Children.Add(rectangle);
Grid.SetRow(rectangle, termin.start + 2);
Grid.SetColumn(rectangle, termin.day * 2 - 1);
Grid.SetColumnSpan(rectangle, 2);
Grid.SetRowSpan(rectangle, termin.end - termin.start);
rectangle.Fill = termin.color;
}
}
}
调查其他类似问题的答案总是避免使用矩形,但理想情况下我想继续使用它们。
您可以将 TextBlock 子项添加到网格,与矩形相同的位置。
您还可以创建一个包含两个子网格的子网格,即 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);
是的,事实证明答案真的很简单,我是个白痴。
我是这样解决的:
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;
}
}
}
您好,我需要能够生成带有文本的动态矩形。我现在遇到了问题,我无法在矩形
上添加文本我在这里生成矩形:
public void ShowAppointements()
{
foreach (Termin termin in MainWindow.termine)
{
if (termin.week == Int32.Parse(txtWeek.Content.ToString()))
{
Rectangle rectangle = new Rectangle();
Kalender.Children.Add(rectangle);
Grid.SetRow(rectangle, termin.start + 2);
Grid.SetColumn(rectangle, termin.day * 2 - 1);
Grid.SetColumnSpan(rectangle, 2);
Grid.SetRowSpan(rectangle, termin.end - termin.start);
rectangle.Fill = termin.color;
}
}
}
调查其他类似问题的答案总是避免使用矩形,但理想情况下我想继续使用它们。
您可以将 TextBlock 子项添加到网格,与矩形相同的位置。
您还可以创建一个包含两个子网格的子网格,即 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);
是的,事实证明答案真的很简单,我是个白痴。 我是这样解决的:
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;
}
}
}