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;