2013-05-29 43 views
1

好吧,這太愚蠢了!我很困惑。我有這個xaml將元素的寬度設置爲容器的其餘部分

<StackPanel Style="{DynamicResource FormPanel}"> 
     <StackPanel> 
      <Label Content="{DynamicResource Label_FirstName}" 
        Target="{Binding ElementName=FirstName}"/> 
      <TextBox x:Name="FirstName" /> 
     </StackPanel> 
     <StackPanel> 
      <Label Content="{DynamicResource Label_LastName}" 
        Target="{Binding ElementName=LastName}"/> 
      <TextBox x:Name="LastName" /> 
     </StackPanel> 
     <!-- and so one... for each row, I have a StackPanel and a Label and Textbox in it --> 
    </StackPanel> 

而這種風格:

<Style x:Key="FormPanel" TargetType="{x:Type StackPanel}"> 
    <Setter Property="Orientation" Value="Vertical"/> 
    <Setter Property="HorizontalAlignment" Value="Stretch" /> 
    <Style.Resources> 
     <Style TargetType="{x:Type StackPanel}"> 
      <Setter Property="Orientation" Value="Horizontal" /> 
      <Setter Property="HorizontalAlignment" Value="Stretch" /> 
      <Setter Property="Margin" Value="10"/> 
      <Style.Resources> 
       <Style TargetType="{x:Type Label}"> 
        <Setter Property="Width" Value="140"/> 
       </Style> 
       <Style TargetType="{x:Type TextBox}"> 
        <!-- this line doesn't affect --> 
        <Setter Property="HorizontalAlignment" Value="Stretch"/> 
       </Style> 
      </Style.Resources> 
     </Style> 
    </Style.Resources> 
</Style> 

我想設置的TextBox.Width到容器的(StackPanel)寬度的其餘部分。在這種情況下,HorizontalAlignment = Stretch不起作用。你有什麼想法嗎?

回答

1

StackPanel只分配所需的子元素比什麼是可用的空間。你需要的是一個DockPanel

看看This對同一主題的一些詳細的解釋。

您可以修改代碼以類似:

<Style x:Key="FormPanel" 
     TargetType="{x:Type StackPanel}"> 
    <Setter Property="Orientation" 
      Value="Vertical" /> 
    <Setter Property="HorizontalAlignment" 
      Value="Stretch" /> 
    <Style.Resources> 
    <Style TargetType="{x:Type DockPanel}"> 
     <Setter Property="HorizontalAlignment" 
       Value="Stretch" /> 
     <Setter Property="Margin" 
       Value="10" /> 
     <Setter Property="LastChildFill" 
       Value="True" /> 
     <Style.Resources> 
     <Style TargetType="{x:Type Label}"> 
      <Setter Property="Width" 
        Value="140" /> 
     </Style> 
     </Style.Resources> 
    </Style> 
    </Style.Resources> 
</Style> 

用法:

<StackPanel Style="{DynamicResource FormPanel}"> 
    <DockPanel> 
    <Label Content="{DynamicResource Label_FirstName}" 
      Target="{Binding ElementName=FirstName}" /> 
    <TextBox x:Name="FirstName" /> 
    </DockPanel> 
    <DockPanel> 
    <Label Content="{DynamicResource Label_LastName}" 
      Target="{Binding ElementName=LastName}" /> 
    <TextBox x:Name="LastName" /> 
    </DockPanel> 
    <!-- and so one... for each row, I have a StackPanel and a Label and Textbox in it --> 
</StackPanel> 

其他:

在你的情況我可能無法做到這一點,雖然。如果你的使用情況是讓每一個具有2列,其中第二列延伸到每行使用所有剩餘空間,而不是有一個StackPanel有一堆DockPanel的裏面N行,你可以只使用一個做這一切Grid

類似:

<Grid Margin="5"> 
    <Grid.Resources> 
    <Style TargetType="{x:Type Label}"> 
     <Setter Property="Margin" 
       Value="5" /> 
    </Style> 
    <Style TargetType="{x:Type TextBox}"> 
     <Setter Property="Margin" 
       Value="5" /> 
    </Style> 
    </Grid.Resources> 
    <Grid.ColumnDefinitions> 
    <ColumnDefinition Width="140" /> 
    <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <Label Grid.Row="0" 
      Grid.Column="0" 
      Content="{DynamicResource Label_FirstName}" 
      Target="{Binding ElementName=FirstName}" /> 
    <TextBox x:Name="FirstName" 
      Grid.Row="0" 
      Grid.Column="1" /> 
    <Label Grid.Row="1" 
      Grid.Column="0" 
      Content="{DynamicResource Label_LastName}" 
      Target="{Binding ElementName=LastName}" /> 
    <TextBox x:Name="LastName" 
      Grid.Row="1" 
      Grid.Column="1" /> 
</Grid> 

會給你只用1佈局容器相同的輸出。

+0

完善建議:d –

+0

@Javad_Amiry我已經編輯我的答案有一個「雜項」部分,如果它直接應用,這可能是爲你的使用情況較好。只需一個就可以完成你所需要的任務,從而避免使用過多的佈局容器。 – Viv

+0

那麼,表單有許多行 - 問題示例只顯示其中的2個 - 我必須定義大量的RowDefinition。另外,我必須爲每個控件設置'Grid.Row'和'Grid.Column',這意味着如果我想重新定位一行,我必須改變很多控件的位置。但我同意你的看法,「網格」似乎是一個更好的容器。對上述問題有任何想法嗎? –

0

StackPanel只會大如它的內容。

我建議你用DockPanel更換內StackPanelDockPanel的最後一個孩子將填充可用空間(除非您顯式覆蓋該行爲)。

相關問題