Search code examples
c#wpfpositiontabcontroltabitem

Position of TextBox inside TabItem (WPF, C#)


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);
  }

Solution

  • 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);