Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中将文本框居中放置在边框内#_C#_Windows Phone 7 - Fatal编程技术网

C# 如何在C中将文本框居中放置在边框内#

C# 如何在C中将文本框居中放置在边框内#,c#,windows-phone-7,C#,Windows Phone 7,在我的WP7应用程序中,我正在边框内创建一个Textbox。如何将文本框精确对齐在边框的中心 Border rectangleborder = new Border(); rectangleborder.Background = new SolidColorBrush(Colors.Transparent); rectangleborder.BorderBrush = new SolidColorBrush(Colors.Black);

在我的
WP7
应用程序中,我正在
边框内创建一个
Textbox
。如何将
文本框
精确对齐在
边框的中心

         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;

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

TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Center;
textbox.VerticalAlignment = VerticalAlignment.Center;
结果应该是这样的:

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

您还可以使用以下方法对内部文本进行allign:-

     textBox.TextAlign = HorizontalAlignment.Center;