2013-04-09 82 views

回答

0

創建新項目並在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; 
} 
+0

我不認爲有必要將邊框的寬度和高度設置爲這些常數值並測量TextBlock。邊框應自動調整大小以適應內容。 – 2013-04-09 16:59:16

+0

如果我沒有設置邊框,那麼它被設置爲整個網格而不是文本塊。 – Xyroid 2013-04-09 17:02:37

+0

除非您將「垂直/水平對齊」屬性設置爲「拉伸」以外的其他屬性。 – 2013-04-09 17:16:46