我有一個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