【问题标题】:Programmatically set textblock margin以编程方式设置文本块边距
【发布时间】:2012-03-14 18:17:58
【问题描述】:

我想知道如何以编程方式设置文本块的边距?我有一个字符串列表,我想将其分配给每个文本块,并在每个文本块之间使用间距为每个文本块设置动画。刚才,所有的文本块都在同一行,所以我看不清文字是什么。

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);
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    您可以像这样设置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);
    

    【讨论】:

      【解决方案2】:

      参考边距属性here

      tb.Margin = new Thickness(10);
      

      【讨论】:

      • 实际上不,这是正确的,但是我必须在添加每个文本块后逐步增加边距,以便文本位于不同的边距上。不管怎么说,还是要谢谢你。 :)
      猜你喜欢
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 2011-01-29
      • 2019-02-08
      • 2019-10-19
      • 1970-01-01
      相关资源
      最近更新 更多