Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set textblock margin

Tags:

c#

wpf

I was wondering how you can set a textblock's margin programmatically? I have a list of strings, that I want to assign to each textblock and animate each one with a spacing in between each textblock. Just now, all the textblocks are on the same line, so I can't make out what the text says.

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);
}
like image 623
Michael Avatar asked Mar 14 '12 18:03

Michael


2 Answers

You can set the Margin property like this:

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

or you can specify a single value which applies to all above:

  double all = 5;
  textBlock.Margin = new Thickness(all);
like image 147
ionden Avatar answered Oct 16 '22 08:10

ionden


Refer to margin property here.

tb.Margin = new Thickness(10);
like image 37
Sofian Hnaide Avatar answered Oct 16 '22 10:10

Sofian Hnaide