我有一个WPF页面,上面有一些数据条目TextBoxes,它们看起来比字体需要大得多。什么决定了文本框的高度?有没有办法把它们挤压?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使得它的尺寸相同。作为临时措施,我只设置一个边距上标签为-2。
当然,它充满了容器空间,问题是为什么它使容器空间如此之大?看起来,当它测量孩子的尺寸时,如果标签与文本框有不同的VerticalAlignment,那么尺寸也会有所不同。 – gimpy 2009-02-20 06:30:45