Align items in a stack panel?

前端 未结 9 1197
我寻月下人不归
我寻月下人不归 2020-12-04 10:23

I was wondering if I can have 2 controls in a horizontal-oriented StackPanel so that the right item should be docked to the right side of the StackPanel.

I tried the

相关标签:
9条回答
  • Maybe not what you want if you need to avoid hard-coding size values, but sometimes I use a "shim" (Separator) for this:

    <Separator Width="42"></Separator>
    
    0 讨论(0)
  • 2020-12-04 11:21

    Yo can set FlowDirection of Stack panel to RightToLeft, and then all items will be aligned to the right side.

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

    Could not get this working using a DockPanel quite the way I wanted and reversing the flow direction of a StackPanel is troublesome. Using a grid is not an option as items inside of it may be hidden at runtime and thus I do not know the total number of columns at design time. The best and simplest solution I could come up with is:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="1" Orientation="Horizontal">
            <!-- Right aligned controls go here -->
        </StackPanel>
    </Grid>
    

    This will result in controls inside of the StackPanel being aligned to the right side of the available space regardless of the number of controls - both at design and runtime. Yay! :)

    0 讨论(0)
提交回复
热议问题