2010-05-17 169 views
1

我很新來WPF和XAML。我試圖設計一個基本的數據輸入表單。我使用了一個堆疊面板來存放四個堆疊面板來獲得我想要的佈局。也許網格會更好,我不確定。WPF格式問題 - 自動拉伸和調整大小?

這是我在行動形式的圖像:http://yfrog.com/7gscreenshot1impp

這裏是產生它的XAML代碼:

<Window x:Class="Test1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="224" Width="536.762"> 
    <StackPanel Height="Auto" Name="stackPanel1" Width="Auto" Orientation="Horizontal"> 
     <StackPanel Height="Auto" Name="stackPanel2" Width="Auto"> 
      <Label Height="Auto" Name="label1" Width="Auto">Patient Name:</Label> 
      <Label Height="Auto" Name="label2" Width="Auto">Physician:</Label> 
      <Label Height="Auto" Name="label3" Width="Auto">Insurance:</Label> 
      <Label Height="Auto" Name="label4" Width="Auto">Therapy Goals:</Label> 
     </StackPanel> 
     <StackPanel Height="Auto" Name="stackPanel3" Width="Auto"> 
      <TextBox Height="Auto" Name="textBox1" Width="Auto" Padding="3" Margin="1" /> 
      <TextBox Height="Auto" Name="textBox2" Width="Auto" Padding="3" Margin="1" /> 
      <TextBox Height="Auto" Name="textBox3" Width="Auto" Padding="3" Margin="1" /> 
      <TextBox Height="Auto" Name="textBox4" Width="Auto" Padding="3" Margin="1" /> 
     </StackPanel> 
     <StackPanel Height="Auto" Name="stackPanel4" Width="Auto"> 
      <Label Height="Auto" Name="label5" Width="Auto">Date:</Label> 
      <Label Height="Auto" Name="label6" Width="Auto">Patient Phone:</Label> 
      <Label Height="Auto" Name="label7" Width="Auto">Facility:</Label> 
      <Label Height="Auto" Name="label8" Width="Auto">Referring Physician:</Label> 
     </StackPanel> 
     <StackPanel Height="Auto" Name="stackPanel5" Width="Auto"> 
      <TextBox Height="Auto" Name="textBox5" Width="Auto" Padding="3" Margin="1" /> 
      <TextBox Height="Auto" Name="textBox6" Width="Auto" Padding="3" Margin="1" /> 
      <TextBox Height="Auto" Name="textBox7" Width="Auto" Padding="3" Margin="1" /> 
      <TextBox Height="Auto" Name="textBox8" Width="Auto" Padding="3" Margin="1" /> 
     </StackPanel> 
    </StackPanel> 
</Window> 

我真正想要的是文本框同樣拉伸以填充水平放置空間。我還希望在垂直調整窗口大小時,每個垂直堆棧面板中的控件均勻分佈。

你們能幫助我嗎?

回答

3

StackPanel始終將其子對準頂部或左邊緣,具體取決於其方向。這聽起來像你想要的是外部StackPanel所在的UniformGrid。試試這個:

請注意,您不需要設置寬度=自動或高度=自動,這是暗示。

但是,你說得對,一個網格可能更好(即使XAML很醜),因爲在這種佈局配置中,你的標籤可能很容易與文本框不一致。你可以嘗試這樣的事情太...

<UniformGrid Rows="1"> 

    <Grid> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 

     <Label Grid.Row="0" Grid.Column="0" Content="Field 1" /> 
     <TextBox Grid.Row="0" Grid.Column="1" /> 


     <Label Grid.Row="1" Grid.Column="0" Content="Field 2" /> 
     <TextBox Grid.Row="1" Grid.Column="1" /> 


     <Label Grid.Row="2" Grid.Column="0" Content="Field 3" /> 
     <TextBox Grid.Row="2" Grid.Column="1" /> 

    </Grid> 

    <Grid /> <!-- repeat above --> 

    <Grid /> <!-- etc... --> 

</UniformGrid> 
+0

感謝上帝我終於找到了答案...... :) +1 !!! – Dave 2010-08-04 17:14:48

1

起飛什麼喬希說,如果你需要控制,以填補空間垂直,以及,你可以定義一些「空間填充」行以分散控制垂直也是如此。這裏有一些XAML演示了這一點(我添加了邊距以將標籤從窗口邊框中移出)。

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <Label Grid.Column="0" Grid.Row="0" Margin="5">Patient Name:</Label> 
    <TextBox Grid.Column="1" Grid.Row="0" Margin="5" /> 
    <Label Grid.Column="0" Grid.Row="2" Margin="5">Physician:</Label> 
    <TextBox Grid.Column="1" Grid.Row="2" Margin="5" /> 
    <Label Grid.Column="0" Grid.Row="4" Margin="5">Insurance:</Label> 
    <TextBox Grid.Column="1" Grid.Row="4" Margin="5" /> 
    <Label Grid.Column="0" Grid.Row="6" Margin="5">Therapy Goals:</Label> 
    <TextBox Grid.Column="1" Grid.Row="6" Margin="5" /> 
    <Label Grid.Column="2" Grid.Row="0" Margin="5">Date:</Label> 
    <TextBox Grid.Column="3" Grid.Row="0" Margin="5" /> 
    <Label Grid.Column="2" Grid.Row="2" Margin="5">Patient Phone:</Label> 
    <TextBox Grid.Column="3" Grid.Row="2" Margin="5" /> 
    <Label Grid.Column="2" Grid.Row="4" Margin="5">Facility:</Label> 
    <TextBox Grid.Column="3" Grid.Row="4" Margin="5" /> 
    <Label Grid.Column="2" Grid.Row="6" Margin="5">Referring Physician:</Label> 
    <TextBox Grid.Column="3" Grid.Row="6" Margin="5" /> 
</Grid>