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不起作用。你有什么想法吗?

4

2 回答 2

1

StackPanel仅分配子元素所需的空间而不是可用空间。你需要的是一个DockPanel.

看看这个关于同一主题的一些详细解释。

您可以将代码修改为:

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

杂项:

在你的情况下,我可能不会这样做。如果您的用例是 N 行,每行有 2 列,其中第二列延伸以使用所有剩余空间,而不是每行StackPanel包含一堆DockPanel',您只需使用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 个布局容器即可为您提供相同的输出。

于 2013-05-29T13:13:10.903 回答
0

StackPanel只会和它的内容一样大。

我建议您将内部替换StackPanelDockPanel. 的最后一个孩子DockPanel将填充可用空间(除非您明确覆盖该行为)。

于 2013-05-29T13:13:54.307 回答