控制子元素的间隔
代码
第一种方式
<StackPanel
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Aqua">
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="0,10,0,0" />
</Style>
</StackPanel.Resources>
<Button Content="BUTTON A" />
<Button Content="BUTTON B" />
<Button Content="BUTTON C" />
</StackPanel>
第二种方式
<StackPanel
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Bisque">
<Separator Height="10" Opacity="0" />
<Button Content="BUTTON A" />
<Separator Height="10" Opacity="0" />
<Button Content="BUTTON B" />
<Separator Height="10" Opacity="0" />
<Button Content="BUTTON C" />
</StackPanel>
效果
为Stackpanel 添加滚动条
代码
<Grid>
<ScrollViewer
x:Name="scrolls"
Width="200"
Height="200"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<ScrollViewer.Content>
<StackPanel
x:Name="numberedItemsStackPanel"
Orientation="Vertical"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<Button Content="BUTTON A" />
<Button Content="BUTTON B" />
<Button Content="BUTTON C" />
<Button Content="BUTTON A" />
<Button Content="BUTTON B" />
<Button Content="BUTTON C" />
<Button Content="BUTTON A" />
<Button Content="BUTTON B" />
<Button Content="BUTTON C" />
<Button Content="BUTTON A" />
<Button Content="BUTTON B" />
<Button Content="BUTTON C" />
</StackPanel>
</ScrollViewer.Content>
</ScrollViewer>
<!-- 属性:Orientation决定显示方向(水平或垂直). -->
</Grid>