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);
  }
1
  • Every FrameworkElement has Margin. 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). Commented May 15, 2017 at 12:19

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

And obviously VerticalAlignment and HorizontalAlignment to make the positioning slightly more dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.