控制子元素的间隔

代码

第一种方式

  1. <StackPanel
  2. HorizontalAlignment="Center"
  3. VerticalAlignment="Center"
  4. Background="Aqua">
  5. <StackPanel.Resources>
  6. <Style TargetType="{x:Type Button}">
  7. <Setter Property="Margin" Value="0,10,0,0" />
  8. </Style>
  9. </StackPanel.Resources>
  10. <Button Content="BUTTON A" />
  11. <Button Content="BUTTON B" />
  12. <Button Content="BUTTON C" />
  13. </StackPanel>

第二种方式

  1. <StackPanel
  2. Grid.Column="1"
  3. HorizontalAlignment="Center"
  4. VerticalAlignment="Center"
  5. Background="Bisque">
  6. <Separator Height="10" Opacity="0" />
  7. <Button Content="BUTTON A" />
  8. <Separator Height="10" Opacity="0" />
  9. <Button Content="BUTTON B" />
  10. <Separator Height="10" Opacity="0" />
  11. <Button Content="BUTTON C" />
  12. </StackPanel>

效果

image.png

为Stackpanel 添加滚动条

代码

  1. <Grid>
  2. <ScrollViewer
  3. x:Name="scrolls"
  4. Width="200"
  5. Height="200"
  6. HorizontalScrollBarVisibility="Auto"
  7. VerticalScrollBarVisibility="Auto">
  8. <ScrollViewer.Content>
  9. <StackPanel
  10. x:Name="numberedItemsStackPanel"
  11. Orientation="Vertical"
  12. ScrollViewer.VerticalScrollBarVisibility="Auto">
  13. <Button Content="BUTTON A" />
  14. <Button Content="BUTTON B" />
  15. <Button Content="BUTTON C" />
  16. <Button Content="BUTTON A" />
  17. <Button Content="BUTTON B" />
  18. <Button Content="BUTTON C" />
  19. <Button Content="BUTTON A" />
  20. <Button Content="BUTTON B" />
  21. <Button Content="BUTTON C" />
  22. <Button Content="BUTTON A" />
  23. <Button Content="BUTTON B" />
  24. <Button Content="BUTTON C" />
  25. </StackPanel>
  26. </ScrollViewer.Content>
  27. </ScrollViewer>
  28. <!-- 属性:Orientation决定显示方向(水平或垂直). -->
  29. </Grid>

效果

image.png