2009-02-18 64 views
4

我有一个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。

回答

13

我的猜测是你r TextBox的容器造成它太大。

尝试在Kaxaml以下XAML:

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Grid VerticalAlignment="Center" HorizontalAlignment="Center"> 
    <TextBox Text="Sample text" FontSize="2" /> 
    </Grid> 

</Page> 

这使得在页面的中心一个非常小的文本框中。如果您从容器中删除VerticalAlignment="Center"HorizontalAlignment="Center",那么文本框非常大。

GridTextBox的默认水平和垂直对齐方式是Stretch,这基本上意味着该元素不关心,并将采取它给出的内容。所以布局引擎询问TextBox它应该是多大,并且没有得到答案,所以布局询问GridGrid并不在乎,最后它会询问具有固定大小的Page/Window,然后将此大小向下传播到可视化树(沿途考虑任何边距和填充)。最终的结果是TextBox填补了整个地区。

为了证明这一点,请将对齐属性从Grid移动到TextBox本身。你不能直观地看到差异,但是如果你设置了Grid的背景颜色,你可以。

<Grid Background="Red"> 
    <TextBox VerticalAlignment="Center" HorizontalAlignment="Center" 
      Text="Sample text" FontSize="2" /> 
</Grid> 

如果您想将所有文本框的边框正对着该文本,也可以在文本框中输入自己设定Padding="0"

有关WPF布局系统的更多信息,请参阅this article

+0

当然,它充满了容器空间,问题是为什么它使容器空间如此之大?看起来,当它测量孩子的尺寸时,如果标签与文本框有不同的VerticalAlignment,那么尺寸也会有所不同。 – gimpy 2009-02-20 06:30:45

0

这里是我的意思的例子...

<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。