Reputation: 1677
Well,
I have created textblock
programmatically in c#. but its not displayed in app. Whats wrong ?
Here's my updated c# code:
double left = 0, top = 15, right = 0, bottom = 0;
double left1 = 0, top1 = 12, right1 = 0, bottom1 = 12;
TextBlock fileName = new TextBlock();
fileName.Margin = new Thickness(left, top, right, bottom);
fileName.FontSize = 30;
fileName.Foreground = new SolidColorBrush(Colors.White);
fileName.TextAlignment = TextAlignment.Center;
fileName.Text = "hello";
StackPanel content = new StackPanel();
content.Margin = new Thickness(left1, top1, right1, bottom1);
content.SetValue(Grid.RowProperty, 0);
content.Children.Add(fileName);;
Upvotes: 1
Views: 620
Reputation: 69372
You've added the TextBlock
to the StackPanel
but you haven't added the StackPanel to the visual tree. Assuming you want to add it to the LayoutRoot
, you can do this
LayoutRoot.Children.Add(content);
As a side note, is there a reason you're doing this programmatically? Depending on your situation, you might be better off using a UserControl
.
Upvotes: 1
Reputation: 63105
you need to add it to a control something like StackPanel
StackPanel1.Children.Add(fileName);
Upvotes: 0