如何为WPF中的多个控件设置堆栈面板内单个单元格的边距?

时间:2014-05-28 07:18:28

标签: wpf xaml jquery-ui-accordion

我需要textble1控件应该由lable1和textbox2控件放在lable2旁边。我正在利用网格中的单个单元格进行整个控制对齐。我无法在任何需要的地方拖动和放置控件。还有其他一些可以设想的方法来将我的控件拖放到网格中的单元格中吗?

这是我的XAML代码..

<Grid Background="#BFBFBF">
        <StackPanel  Margin="2,2,2,2"  >
            <Grid Margin="2,2,2,2"    VerticalAlignment="Top">
                <Grid.RowDefinitions >
                    <RowDefinition Height="35*"/>
                    <RowDefinition Height="35*"/>
                    <RowDefinition Height="35*"/>
                </Grid.RowDefinitions>

               <!-- 1st Content-->
               <Border  Margin="4,2,2,2"  BorderBrush="#FF0100A3" Grid.Row="0"    CornerRadius="3,3,3,3"   VerticalAlignment="Top" BorderThickness="2,2,2,2" Height="30"   MouseLeftButtonDown="brdSectionOne_MouseLeftButtonDown">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF5E68CE" Offset="0"/>
                        <GradientStop Color="#FF5B64CA" Offset="1"/>
                        <GradientStop Color="#FF000C82" Offset="0.356"/>
                    </LinearGradientBrush>
                </Border.Background>
                <TextBlock Text="Section 1" Grid.ColumnSpan ="2"  Foreground="White" Grid.Row="0" FontFamily="Segoe UI" FontSize="13"  Margin="8,5,2,0" VerticalAlignment="Top" FontWeight="Normal" Cursor="Hand" />
            </Border>
            <StackPanel x:Name="spnlFirstSection"  Grid.Row="0"  VerticalAlignment="Bottom"  Height="436" Margin="3,35,3,0"  >
                <Border  Margin="1,1,1,1"  CornerRadius="3,3,3,3"   VerticalAlignment="Top" BorderThickness="2,2,2,2" Height="430" Background="#FFFFFFFF" BorderBrush="#FF4C4BEE" >
                    <StackPanel Grid.ColumnSpan ="2" Grid.Row="0" Height="445" Margin="6,6,6,0" VerticalAlignment="Top" >
                        <Label x:Name="lbl_1" Content="Label 1" Margin="50,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="-1.895,0" FontSize="14"/>
                        <TextBox x:Name="textbox1" Margin="300,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Text="textbox1" Height="25" TextWrapping="Wrap" Width="250" FontSize="18"/>
                        <Label x:Name="lbl_2" Content="Label 2" Margin="50,30,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="-1.895,0" FontSize="14"/>
                        <TextBox x:Name="textbox2" Margin="300,30,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Text="textbox2" Height="25" TextWrapping="Wrap" Width="250" FontSize="18"/>
                    </StackPanel>
                    <!--<TextBlock Text="Section 1 Content" Grid.ColumnSpan ="2"  Foreground="#FF000000" Grid.Row="0" FontFamily="Segoe UI" FontSize="13"  Margin="6,6,6,0" VerticalAlignment="Top" FontWeight="Normal" Cursor="Hand" Height="445" />-->
                </Border>
            </StackPanel>
      </Grid>
    </StackPanel>
  </Grid>

1 个答案:

答案 0 :(得分:1)

StackPanel垂直(默认)或水平堆叠。像这样:

<StackPanel>
    <TextBlock Text="Foo"/>
    <TextBlock Text="Bar"/>
</StackPanel>

将呈现:

虽然

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Foo"/>
    <TextBlock Text="Bar"/>
</StackPanel>

将呈现:
Foo Bar

如果你想看起来像网格,你应该使用网格!所以,如果你想要这样的东西:
LabelOne:Foo
LabelTwo:Bar

<Grid>
    <Grid.ColumnDefinitions>
       <ColumnDefinition Width="Auto" />
       <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
       <RowDefinition Height="*" />
       <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Label Content="LabelOne:" /> <!-- Grid.Row and Grid.Column default to 0 -->
    <Label Content="LabelTwo:" Grid.Row="1" />
    <TextBox Text="Foo" Grid.Column="1" />
    <TextBox Text="Bar" Grid.Row="1" Grid.Column="1" />
</Grid>

你也可以试着像这样嵌套StackPanels:

<StackPanel Orientation="Horizontal">
    <StackPanel>
        <Label Content="LabelOne:" />
        <Label Content="LabelTwo:" />
    </StackPanel>
    <StackPanel>
        <TextBox Text="Foo" />
        <TextBox Text="Bar" />
    </StackPanel>
</StackPanel>

但这可能会让你有些不受欢迎的行为。