2009-02-18 43 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。