2012-09-15 87 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;