How do I space out the child elements of a StackPanel?

前端 未结 11 2108
长情又很酷
长情又很酷 2020-12-04 06:11

Given a StackPanel:


  Apple
  Banana
  

        
相关标签:
11条回答
  • 2020-12-04 07:07

    Use Margin or Padding, applied to the scope within the container:

    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Margin" Value="0,10,0,0"/>
            </Style>
        </StackPanel.Resources> 
        <TextBox Text="Apple"/>
        <TextBox Text="Banana"/>
        <TextBox Text="Cherry"/>
    </StackPanel>
    

    EDIT: In case you would want to re-use the margin between two containers, you can convert the margin value to a resource in an outer scope, f.e.

    <Window.Resources>
        <Thickness x:Key="tbMargin">0,10,0,0</Thickness>
    </Window.Resources>
    

    and then refer to this value in the inner scope

    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Margin" Value="{StaticResource tbMargin}"/>
        </Style>
    </StackPanel.Resources>
    
    0 讨论(0)
  • 2020-12-04 07:09

    sometimes you need to set Padding, not Margin to make space between items smaller than default

    0 讨论(0)
  • 2020-12-04 07:11

    The UniformGrid might not be available in Silverlight, but someone has ported it from WPF. http://www.jeff.wilcox.name/2009/01/uniform-grid/

    0 讨论(0)
  • 2020-12-04 07:13

    Usually, I use Grid instead of StackPanel like this:

    horizontal case

    <Grid>
     <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition  Width="auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition  Width="auto"/>
     </Grid.ColumnDefinitions>
     <TextBox Height="30" Grid.Column="0">Apple</TextBox>
     <TextBox Height="80" Grid.Column="2">Banana</TextBox>
     <TextBox Height="120" Grid.Column="4">Cherry</TextBox>
    </Grid>
    

    vertical case

    <Grid>
         <Grid.ColumnDefinitions>
            <RowDefinition Width="auto"/>
            <RowDefinition Width="*"/>
            <RowDefinition  Width="auto"/>
            <RowDefinition Width="*"/>
            <RowDefinition  Width="auto"/>
         </Grid.ColumnDefinitions>
         <TextBox Height="30" Grid.Row="0">Apple</TextBox>
         <TextBox Height="80" Grid.Row="2">Banana</TextBox>
         <TextBox Height="120" Grid.Row="4">Cherry</TextBox>
    </Grid>
    
    0 讨论(0)
  • 2020-12-04 07:18

    My approach inherits StackPanel.

    Usage:

    <Controls:ItemSpacer Grid.Row="2" Orientation="Horizontal" Height="30" CellPadding="15,0">
        <Label>Test 1</Label>
        <Label>Test 2</Label>
        <Label>Test 3</Label>
    </Controls:ItemSpacer>
    

    All that's needed is the following short class:

    using System.Windows;
    using System.Windows.Controls;
    using System;
    
    namespace Controls
    {
        public class ItemSpacer : StackPanel
        {
            public static DependencyProperty CellPaddingProperty = DependencyProperty.Register("CellPadding", typeof(Thickness), typeof(ItemSpacer), new FrameworkPropertyMetadata(default(Thickness), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCellPaddingChanged));
            public Thickness CellPadding
            {
                get
                {
                    return (Thickness)GetValue(CellPaddingProperty);
                }
                set
                {
                    SetValue(CellPaddingProperty, value);
                }
            }
            private static void OnCellPaddingChanged(DependencyObject Object, DependencyPropertyChangedEventArgs e)
            {
                ((ItemSpacer)Object).SetPadding();
            }
    
            private void SetPadding()
            {
                foreach (UIElement Element in Children)
                {
                    (Element as FrameworkElement).Margin = this.CellPadding;
                }
            }
    
            public ItemSpacer()
            {
                this.LayoutUpdated += PART_Host_LayoutUpdated;
            }
    
            private void PART_Host_LayoutUpdated(object sender, System.EventArgs e)
            {
                this.SetPadding();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题