本文介绍了Silverlight:TextBox VerticalContentAlignment="Center"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 VerticalContentAlignment 属性将 TextBox 的内容垂直居中,但它似乎根本没有效果.文本保持在顶部.谁能告诉我怎么做?

I'm trying to vertically center the content of a TextBox with the VerticalContentAlignment property but it seems to have no effect at all. The text stays at the top. Can anyone tell me how to do this?

这是我的代码:

<TextBox Grid.Column="1"
     Grid.Row="0"
     Width="200"
     Height="28"
     VerticalAlignment="Center"
     VerticalContentAlignment="Center" />

推荐答案

可以使 TextBox 的文本垂直居中.但是,这确实需要您重新应用其 ControlTemplate.

It is possible to make the TextBox center its text vertically. However, that does require you to reapply its ControlTemplate.

要做到这一点:

  1. 从 MSDN 上的 TextBox 样式和模板页面复制样式和 ControlTemplate 到合适的 元素.(此 ControlTemplate 实际上用于验证工具提示;我们将更改的 ControlTemplate 在 Style 内.)
  2. 在 TextBox 的 Style 中找到 ScrollViewer 元素,并向其添加 VerticalAlignment="Center" 属性.
  1. Copy the Style and the ControlTemplate from the TextBox Styles and Templates page on MSDN to a suitable <UserControl.Resources> element. (This ControlTemplate is actually for a validation tooltip; the ControlTemplate we'll change is within the Style.)
  2. Find the ScrollViewer element within the Style for the TextBox, and add a VerticalAlignment="Center" property to it.

或者,您可以添加属性

VerticalAlignment="{TemplateBinding VerticalContentAlignment}"

到滚动查看器.这应该允许您使用 VerticalContentAlignment 属性设置文本框内容的垂直对齐方式.

to the ScrollViewer. This should allow you to set the vertical alignment of the contents of your TextBoxes using the VerticalContentAlignment property.

如果您也希望更改 TextBox 内容的水平对齐方式,您可以采用大致相同的方法.

You can follow much the same approach if you wish to change the horizontal alignment of a TextBox's content as well.

这篇关于Silverlight:TextBox VerticalContentAlignment="Center"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:14