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);
}
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);
Refer to margin property here.
tb.Margin = new Thickness(10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With