我有一个 WPF 页面,上面有一些数据输入文本框,它们看起来比字体需要的大得多。什么决定了文本框的高度?有没有办法把它们压扁?
文本框根据它显示的字体大小变得越来越小(所以如果我可以帮助的话,我不想直接设置 height 属性。
这是我的意思的一个例子......
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="LabelStyle" TargetType="Label">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
</Page.Resources>
<StackPanel>
<WrapPanel>
<Label Style="{StaticResource LabelStyle}" Content="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>
<TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
</WrapPanel>
<WrapPanel>
<Label Style="{StaticResource LabelStyle}" Content="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>
<TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
</WrapPanel>
</StackPanel>
</Page>
如果您查看大小,您会看到标签比文本框大一点。将文本框上的 VerticalAlignment 更改为 Top 使其大小相同。作为一项临时措施,我只是将标签上的边距设置为 -2。