0

I creating TabItems dynamically. Inside TabItem I want to add TextBox.

How can I set up position of TextBox?

GenerateTabControlModel gtcm = new GenerateTabControlModel();       

 for (int x = 0; x <= gtcm.getTabNumber();x++)
  {
   TabItem tab = new TabItem();
   tab.Header = x.ToString();
   tab.Width = 30;
   tab.Height = 20;
   string sometext = "tab number: " + x.ToString();

   TextBox tb = new TextBox();
   tb.Text = sometext;                
   tb.Height = 25;
   tb.Width = 120;

   tab.Content = tb;               

   TCDynamo.Items.Add(tab);
  }
4est
  • 3,010
  • 6
  • 41
  • 63
  • Every `FrameworkElement` has [`Margin`](https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.margin(v=vs.110).aspx). Though consider to use dynamic layouting (`Margin` is still used in it, but it's not something like `Margin = "500,300,20,10`), then you need a proper parent container (Grid + column/row definitions, StackPanel, WrapPanel, etc.) and utilizing alignment properties: `VerticalAlignment` and `HorizontalAlignment` (e.g. [centering](http://stackoverflow.com/a/1313975/1997232)). – Sinatr May 15 '17 at 12:19

1 Answers1

1

Using Margin property. Let's say you want to position your TextBox at { X: 20, Y: 35 } :

tb.Margin = new Thickness (20, 35, 0, 0);

Alternatively if it's parent is Canvas you can use Canvas.Left and Canvas.Top properties :

Cavnas.SetLeft(tb, 20);
Canvas.SetTop(tb, 35);

Another alternative is to use RenderTransform or LayoutTransform and set TranslateTransform into these properties :

tb.RenderTransform = new TranslateTransform(20, 35);
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
  • And obviously `VerticalAlignment` and `HorizontalAlignment` to make the positioning slightly more dynamic. – J R May 15 '17 at 15:45