2

在我的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;
4

2 回答 2

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

您还可以使用以下方式对齐文本:-

     textBox.TextAlign = HorizontalAlignment.Center;
于 2012-09-15T14:54:12.763 回答
1

您需要将 设置HorizontalAlignment为水平对齐,将设置VerticalAlignment为垂直对齐:

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

结果应该是这样的:

在此处输入图像描述

于 2012-09-15T14:51:56.987 回答