【问题标题】:TextBox vertical alignment in Windows Store AppWindows 应用商店应用中的文本框垂直对齐
【发布时间】:2015-10-17 10:47:47
【问题描述】:

我正在尝试做一些看起来很简单但到目前为止我无法做到的事情:垂直对齐 Windows Store App (XAML) 中 TextBox 中的文本。

VerticalContentAlignment 不起作用(尽管它在 WPF 中运行良好)。我什至尝试提取模板并进行更改。还是做不到。

有人知道怎么做吗?

【问题讨论】:

  • 在 Row 中为网格添加文本框并对齐网格行。<TextBox Text="TestText" Height="80" Grid.Row="1"/>

标签: c# xaml windows-store-apps winrt-xaml


【解决方案1】:

不幸的是,没有简单的方法。但是,如果您的 TextBox 具有固定大小,那么您可以以自定义样式调整 Padding 属性,也可以动态完成,但工作量更大。

 <Style TargetType="TextBox" x:Key="CustomTextBoxStyle">
        <Setter Property="Padding"
                Value="25" />
 </Style>
<TextBox Text="TestText"
             Height="80"
             Style="{StaticResource CustomTextBoxStyle}" />

【讨论】:

  • 好的。我创建了一个与 TextBox 具有相同文本属性(字体系列、大小、样式、粗细)的虚拟 TextBlock,得到它的 ActualHeight,从 TextBox 的高度中减去它并除以 2,最后减去 TextBox 的边框厚度。我将此值设置为填充。丑陋的做法,但它的工作原理。谢谢。
【解决方案2】:

你也可以试试这个……

<Grid>
<TextBox Text="Centered Text" TextAlignment="Center" VerticalAlignment="Center"/></Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2022-03-04
    • 2013-09-15
    相关资源
    最近更新 更多