0

我想在 Windows 应用商店应用程序中以编程方式在 TextBlock 周围添加边框。我怎样才能做到这一点?谢谢。

4

1 回答 1

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;
}
于 2013-04-09T10:47:36.847 回答