以编程方式设置文本块边距

7

我想知道如何在程序中设置文本块的边距?我有一个字符串列表,我想将每个文本块分配给其中的每一个,并在每个文本块之间进行动画处理。现在,所有文本块都在同一行上,所以我无法看清文字内容。

foreach (var i in item.Items)
{
    TextBlock tb = new TextBlock();
    tb.Height = 50;
    tb.Width = 900;
    tb.Text = i.Title + "\n";

    SlideDown(tb);
    canvas.Children.Add(tb);
}

public void SlideDown(FrameworkElement uc)
{
    ThicknessAnimation tAnimation = new ThicknessAnimation();
    tAnimation.Duration = new Duration(TimeSpan.FromSeconds(5.0));
    tAnimation.From = new Thickness(0,0,0,0);
    tAnimation.To = new Thickness(0, 500, 0, 500);
    Storyboard.SetTarget(tAnimation, uc);
    Storyboard.SetTargetProperty(tAnimation, new PropertyPath(FrameworkElement.MarginProperty));
    Storyboard storyboard = new Storyboard();
    storyboard.Children.Add(tAnimation);
    storyboard.Begin(uc);
}
2个回答

21

您可以像这样设置 Margin 属性:

  double left = 1, top = 2, right = 3, bottom = 4;
  textBlock.Margin = new Thickness(left, top, right, bottom);

或者您可以指定一个适用于所有上面的单个值:

  double all = 5;
  textBlock.Margin = new Thickness(all);

3

请参考边距属性这里

tb.Margin = new Thickness(10);

1
实际上不是,这是正确的,但我必须在添加每个文本块后逐步增加边距,以便文本位于不同的边距上。无论如何,谢谢。 :) - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接