我有一个WPF页面,上面有一些数据输入文本框,它们的大小比字体需要的要大得多。是什么决定了文本框的高度?有没有办法把它们压缩起来?
文本框的大小会根据显示的字体大小而变化(因此如果可能的话,我不想直接设置高度属性)。
这里是我所说的例子...
<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。