0
我想在Windows Store应用程序中以编程方式在TextBlock周围添加边框。 我该怎么做? 谢谢。TextBlock的背景边框
我想在Windows Store应用程序中以编程方式在TextBlock周围添加边框。 我该怎么做? 谢谢。TextBlock的背景边框
创建新项目并在OnNavigatedTo
事件中添加此代码。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var tb = new TextBlock()
{
Text = "...TextBlock with border...",
FontSize = 20
};
//To get actual height and width of TextBlock
tb.Arrange(new Rect(0, 0, Window.Current.Bounds.Width, Window.Current.Bounds.Height));
tb.Measure(new Size(Window.Current.Bounds.Width, Window.Current.Bounds.Height));
var border = new Border()
{
BorderThickness = new Thickness(3),
BorderBrush = new SolidColorBrush(Windows.UI.Colors.Red),
Height = tb.ActualHeight + 10,
Width = tb.ActualWidth + 10
};
var rootGrid = (Grid)(((Page)this).Content);
rootGrid.Children.Add(border);
border.Child = tb;
}
我不认为有必要将边框的宽度和高度设置为这些常数值并测量TextBlock。边框应自动调整大小以适应内容。 – 2013-04-09 16:59:16
如果我没有设置边框,那么它被设置为整个网格而不是文本块。 – Xyroid 2013-04-09 17:02:37
除非您将“垂直/水平对齐”属性设置为“拉伸”以外的其他属性。 – 2013-04-09 17:16:46