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