2013-04-09 83 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