2012-09-15 66 views
2

在我的WP7應用程序中,我在Border內部創建了一個Textbox。如何將Textbox準確對齊於Border的中心?如何將文本框置於C中的邊框內#

  Border rectangleborder = new Border(); 
     rectangleborder.Background = new SolidColorBrush(Colors.Transparent); 
     rectangleborder.BorderBrush = new SolidColorBrush(Colors.Black); 
     rectangleborder.BorderThickness = new Thickness(2); 
     rectangleborder.Width = width; 
     rectangleborder.Height = width; 
     TextBox textbox = new TextBox(); 
     textbox.Text = "1"; 
     textbox.Background = new SolidColorBrush(Colors.Transparent); 
     textbox.Foreground = new SolidColorBrush(Colors.Yellow); 
     textbox.BorderBrush = new SolidColorBrush(Colors.Transparent); 
     this.canvas1.Children.Add(rectangleborder); 
     rectangleborder.SetValue(Canvas.LeftProperty, 30 + (j - 1) * width); 
     rectangleborder.SetValue(Canvas.TopProperty, 30 + (i - 1) * width); 
     rectangleborder.Child = textbox; 

回答

1

您需要設置HorizontalAlignment水平對齊和VerticalAlignment垂直對齊:

TextBox textbox = new TextBox(); 
textbox.HorizontalAlignment = HorizontalAlignment.Center; 
textbox.VerticalAlignment = VerticalAlignment.Center; 

而且結果應該是這個樣子:

enter image description here

2
TextBox textbox = new TextBox(); 
    textbox.HorizontalAlignment = HorizontalAlignment.Center; 
    textbox.VerticalAlignment = VerticalAlignment.Center; 

你也可以對我們內部的文字進行排列ing: -

 textBox.TextAlign = HorizontalAlignment.Center;