在我的WP7
应用中,我在Textbox
内创建Border
。如何将Textbox
完全对齐Border
的中心?
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;
答案 0 :(得分:2)
TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Center;
textbox.VerticalAlignment = VerticalAlignment.Center;
您还可以使用以下方式对齐文本: -
textBox.TextAlign = HorizontalAlignment.Center;
答案 1 :(得分:1)
您需要将HorizontalAlignment
设置为水平对齐,VerticalAlignment
设置为垂直对齐:
TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Center;
textbox.VerticalAlignment = VerticalAlignment.Center;
结果看起来应该是这样的: